in the php
file I am creating a 2d
dynamic array and while returning it am encoding using json_encode
$ans=array('x'=>array(),'y'=>array(),'z'=>array());
foreach ($result as &$y)
{ if(strpos($y,"x")){$ans['z'][]=substr(strstr($y,':'),12);}
else if(strpos($y,"y")){$ans['y'][]=substr(strstr($y,':'),2);}
else if(strpos($y,"z")){$ans['z'][]=substr(strstr($y,':'),2);}
}//$y is a very big string
return json_encode($ans);
the output in browser shows as {...}
I.E. jsonobject
but when in java I try to convert httpentity to string(using entityutils.tostring)
and then make
jsonobject ja=new jsonobject(entityutils.tostring(entity));
shows following
error : org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
why is it showing this?
edited :
List<NameValuePair> namevaluepair=new ArrayList<NameValuePair>(1);
namevaluepair.add(new BasicNameValuePair("request",""+x));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost/webc.php");
httppost.setEntity(new UrlEncodedFormEntity(namevaluepair));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if(entity!=null)
{String line=EntityUtils.toString(entity);
JSONObject ja=new JSONObject(line);//showing error here
}