I’m using HTTP Get to retrieve a JSON formatted String from my website.
However, when I try to create the JSONObject, nothing happens. The object isn’t created and I can’t acces Arrays or anything inside it.
I’ve checked that my app does get the right String and it is correctly formatted as a JSON.
Do I have to declare it in some particular way or have separate try/catch blocks…?
I’m at my wits end with this, so any help would be most appreciated…
Lower you have all the code I’ve written in this issue and although it’s not much, I’m stuck and don’t know where to go with this…
public String result, testString;
public TextView resultText;
public String url = "http://www.ace.ucv.ro/android/android.php";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu);
resultText = (TextView) findViewById(R.id.result);
try {
HttpGet httpGet = new HttpGet(url);
result = EntityUtils.toString(new DefaultHttpClient().execute(httpGet).getEntity());
JSONObject jsonObject = new JSONObject(result);
} catch (ParseException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
resultText.setText(result);
}
The URL you have in code (http://www.ace.ucv.ro/android/android.php) does not return Json object, instead it is a JSONArray.
Just replace JSONObject with JSONArray and you’ll be fine.