2

。我有一行 json 数据@我的网络服务。所有 Json 数据都以这种格式提供,只有 URL 和链接。据我所知,它是一个 JsonObject 。Short 说我请求,结果总是以 url 结尾。所以输出是:

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

这就是我所做的

            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;
        }

这里发生错误

logcat 告诉我这个:

JSONObject 无法转换为 jsonarray

那么我怎样才能拥有链接而不是错误呢?

更新 #1 --> Logcat 完全错误

    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

更新 #2 --> JsonParser 类

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;
        }
         }
4

3 回答 3

1

你得到的是 JSONObject 而不是 JSONArray。

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

所以改为

  JSONObject jsonobject= jParser.getJSONFromUrl(url);

解析

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

编辑:

你需要改变这个

 jsonarr = new JSONArray(json);

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

并返回

 return job;  
于 2013-10-01T11:31:23.290 回答
1

您从响应中获得了一个 Json 对象。与{"Url":"www.google.com"}JSONObject 一样。

所以这条线

JSONArray json = jParser.getJSONFromUrl(url);

应该是

JSONObject json = jParser.getJSONFromUrl(url);

在读取数据时,您只需要

String json_url = json.getString(TAG_Url);

而不是使用 for 循环。


查看更新的课程

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
    }
}

这将适用于您当前的 json。看看// Updated here我更新了什么。

于 2013-10-01T11:31:26.690 回答
1

你在课堂上的错误JSONParser,你正在返回JSONArray并且你试图得到JSONObeject。请参阅以下代码。

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;
        }
         }

然后使用以下代码。

JSONObject json = jParser.getJSONFromUrl(url);

String url = json.getString("url");
于 2013-10-01T11:45:33.910 回答