Retrieving JSON from URL on Android

前端 未结 6 1785
无人共我
无人共我 2020-12-03 12:56

My phone APP downloads content perfectly in a text mode. Below is a code to do that. I call Communicator class and exectueHttpGet:

URL_Data = new Communicator(

6条回答
  •  有刺的猬
    2020-12-03 13:28

    What you receive is a series of characters from the InputStream that you append to a StringBuffer and convert to String at the end - so the result of String is ok :)

    What you want is to post-process this String via org.json.* classes like

    String page = new Communicator().executeHttpGet("Some URL");
    JSONObject jsonObject = new JSONObject(page);
    

    and then work on jsonObject. As the data you receive is an array, you can actually say

    String page = new Communicator().executeHttpGet("Some URL");
    JSONArray jsonArray = new JSONArray(page);
    for (int i = 0 ; i < jsonArray.length(); i++ ) {
      JSONObject entry = jsonArray.get(i);
      // now get the data from each entry
    }
    

提交回复
热议问题