0

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
}
  • Because the text you're trying to supply to the `new jsonobject(...)` doesn't start with `{`, try to log each step of your code and it will become clear enough to find an error. – mdolbin Nov 21 '13 at 06:44
  • when seeing output in browser it does start with { – user2966265 Nov 21 '13 at 07:08
  • you might see `{` but it could be an html code of opening brace which will look the same `{` http://www.ascii.cl/htmlcodes.htm . Once more, trace your steps with log\System.out.println and ensure that you actually get json without trash – mdolbin Nov 21 '13 at 07:29
  • i get your point but when am not using json here and just seeing size of line it shows 48(whereas what am sending is quite big) – user2966265 Nov 21 '13 at 08:24

0 Answers0