Hi. I have a line of json data @ my webservice. All Json data is available at this format, only URL and a link. so its a JsonObject as I know. Short said I request and the result always ends with a url. So output is :

{"Url":"www.google.com"}

This what I have done

            JSONArray json = jParser.getJSONFromUrl(url);

            try {
                ListBasedList.clear();
                //for each loop til JSON data
                   for(int i = 0; i < json.length(); i++){
                        JSONObject c = json.getJSONObject(i);

                    String json_url = c.getString(TAG_Url);

                    if(json_url.equals(0) && json_url.equals(""))
                    {
                         LinearLayout lin_footer = (LinearLayout) findViewById(R.id.footer_layoutMain);
                         lin_footer.setVisibility(View.GONE);
                    }

                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put(TAG_Url, json_url);

                    ListBasedList.add(map);
                   }
            } catch (JSONException e) {
                e.printStackTrace();
                Log.e("JSON Parser fejl", "fejl da man prøve og hente data fra server " + e.toString());
            }
            return null;
        }

Error happening here

logcat telling me this :

JSONObject cannot be converted to jsonarray

So How I can have the link instead of the error?

Update #1 --> Logcat full Error

    10-01 13:34:45.685: E/JSON Parser(24256): Error parsing data org.json.JSONException: Value {"url":"www.google.com"} of type org.json.JSONObject cannot be converted to JSONArray

Update #2 --> JsonParser class

public class JSONParser {
    static InputStream is = null;
    static String json = "";
    JSONArray jsonarr=null;
    // konstruktor
    public JSONParser() {
    }

    public JSONArray getJSONFromUrl(String url) {
         JSONArray jsonarr=null;

         // HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();           

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // json array paser til string
          try {
                jsonarr = new JSONArray(json);
                } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // retunerer json object tilbage
        return jsonarr;
        }
         }
有帮助吗?

解决方案

You are getting a single Json object from response. As {"Url":"www.google.com"} is a JSONObject.

So the line

JSONArray json = jParser.getJSONFromUrl(url);

should be as

JSONObject json = jParser.getJSONFromUrl(url);

And while reading the data you only need

String json_url = json.getString(TAG_Url);

instead of using for loop.


See the updated class

public class JSONParser {
    static InputStream is = null;
    static String json = "";
    JSONObject jsonObject = null; // Updated here

    // konstruktor
    public JSONParser() {
    }

    public JSONObject getJSONFromUrl(String url) {
        jsonObject = null; // Updated here

        // HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // json array paser til string
        try {
            jsonObject = new JSONObject(json); // Updated here
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // retunerer json object tilbage
        return jsonObject; // Updated here
    }
}

This will work for your current json. See // Updated here to know what I have updated.

其他提示

What you get is a JSONObject not a JSONArray.

 { // represetns json object node 
 "Url":"www.google.com"
 }

SO change to

  JSONObject jsonobject= jParser.getJSONFromUrl(url);

To parse

  String url = jsonobject.getString("Url");

Edit:

You need to change this

 jsonarr = new JSONArray(json);

To

 JSONObject job = new JSONObject(json); // considering you get the above json

and return

 return job;  

Your mistake in JSONParser class where you are returning JSONArray and you are trying to get JSONObeject. See following code.

public class JSONParser {
    static InputStream is = null;
    static String json = "";
    JSONObject jsonarr=null;
    // konstruktor
    public JSONParser() {
    }

    public JSONObject getJSONFromUrl(String url) {// Change return type from `JSONArray` to `JSONObject`
         JSONObject jsonarr=null;

         // HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();           

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // json array paser til string
          try {
                jsonarr = new JSONObject(json);
                } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // retunerer json object tilbage
        return jsonarr;
        }
         }

Then use following code.

JSONObject json = jParser.getJSONFromUrl(url);

and

String url = json.getString("url");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top