0

我有一个 url,我使用JsonParser类解析 json 内容。我想显示来自网络服务的图像。当我使用 json 对象解析数据时,我无法获取值,并且 logcat ( JsonException) 中显示错误:

public String PostConnection(String strUrl,ArrayList<NameValuePair> alstNameValuePair) {
            InputStream mInputStream = null;
            try {
                //This is the default apacheconnection.
                HttpClient mHttpClient = new DefaultHttpClient();
    //          HttpConnectionParams.setConnectionTimeout(mHttpClient.getParams(), 60000); //Timeout Limit
                //Pathe of serverside 
    //          HttpPost mHttpPost = new HttpPost(strUrl);
                HttpGet mHttpGet = new HttpGet(strUrl);

    //          if(alstNameValuePair!=null)
    //          { 
                    //post the valur you want to pass.
    //               mHttpGet.setEntity(new UrlEncodedFormEntity(alstNameValuePair));
    //          }

                //get the valu from the saerverside as response.
                HttpResponse mHttpResponse = mHttpClient.execute(mHttpGet);
                HttpEntity mHttpEntity = mHttpResponse.getEntity();
                mInputStream = mHttpEntity.getContent();

              } 
              catch (Exception e) {
                  e.printStackTrace();
              }

             String strLine = null;
             String strResult = "";

            //convert response in to the string.
            try {
                    if(mInputStream!=null){
                      BufferedReader mBufferedReader = new BufferedReader(new InputStreamReader(mInputStream,HTTP.UTF_8), 8);
                      StringBuilder mStringBuilder = new StringBuilder();
                      while((strLine = mBufferedReader.readLine()) != null) {
                        mStringBuilder.append(strLine + "\n");
                      }
                          strResult = mStringBuilder.toString();
                          mInputStream.close();
                    }
               } 
               catch (Exception e) {
                    e.printStackTrace();
                }

             return strResult;
        }

这是我的MainActivity.java课程代码

    ArrayList<NameValuePair> mNameValuePair = new ArrayList<NameValuePair>();
                mNameValuePair.add(new BasicNameValuePair("id_product", "id_product"));
                Log.i("NameValuePair","" + mNameValuePair);
                JSONParser jparser = new JSONParser();
                result = jparser.PostConnection(URL1, null);
                Log.i("result",""+ result);

                JSONObject json = new JSONObject();
                json.put(result, true);
                Log.i("JSON FROM URL","" +  json);

    //          String jb = json.get("id_product").toString();
    //          Log.i("jb string","" + jb);


                JSONArray jArray = json.names();
                Log.i("JSON ARRAY","" + jArray);

                String jb = jArray.get(0).toString();
                Log.i("jb string","" + jb);
                ArrayList<String> ararylst = new ArrayList<String>();
                ararylst.add(result);
                Log.i("ArrayList for result",""+ ararylst);


                for(int i=0;i<=jArray.length();i++){

                    Log.i("Json Length",""+ jArray.length());
    //              JSONObject jo = json.getJSONObject("image");
    //              Log.i("JSON OBject in for",""+ jo);
    //              image = jo.getString("image");
    //              Log.i("String in for",""+ image);
    //              json.get("image");
                    JSONObject tableData = jArray.getJSONObject(i);
                    image = tableData.getString("image");
                    Log.i("imageinloop",""+ image);
                    arraylist.add(image);
                    Log.i("ArrayList","" +arraylist);

    //              AddObjectToList(image);

                }

            }
             catch (Exception e) {
                // TODO: handle exception
                 e.printStackTrace();
            }

这是我的 Json

[
    {
        "id_product": "4549",
        "product_code": "9788184402070",
        "id_author": "70",
        "image": "iblazing.net/navbharat/img/p/9/0/9/7/9097-tonytheme_product.jpg"
  },

 {
        "id_product": "4550",
        "product_code": "9788184402071",
        "id_author": "71",
        "image": "iblazing.net/navbharat/img/p/9/0/9/7/9097-tonytheme_product1.jpg"
  }
]

这是我的 Logcat 错误:

11-12 06:04:59.072: W/System.err(2748): org.json.JSONException: Value [{"id_product":"4549","product_code":"9788184402070","id_author":"70","id_editor":"70","id_translator":"70","id_supplier":"0","id_booklang":"1","id_baseunit":"0","id_distributor":"1","id_publisher":"1","id_manufacturer":"0","id_category_default":"2","id_shop_default":"1","id_tax_rules_group":"1","on_sale":"0","online_only":"0","ean13":"","upc":"","ecotax":"0.000000","quantity":"-1","minimal_quantity":"1","price":"135.000000","wholesale_price":"0.000000","unity":"","unit_price_ratio":"0.000000","additional_shipping_cost":"0.00","reference":"","supplier_reference":"","location":"","width":"0.000000","height":"0.000000","depth":"0.000000","weight":"0.000000","out_of_stock":"2","quantity_discount":"0","customizable":"0","uploadable_files":"0","text_fields":"0","active":"1","redirect_type":"404","id_product_redirected":"0","available_for_order":"1","available_date":"0000-00-00","condition":"new","show_price":"1","indexed":"1","visibility":"both","cache_is_pack":"0","cache_has_attachments":"0","is_virtual":"0","cache_default_attribute":"0","no_of_pages":"224","bindingtype":"paper","upcoming":"0","booktype":"ebook","date_add":"2013-10-16 15:23:46","date_upd":"2013-10-25 14:13:54","advanced_stock_management":"0","id_shop":"1","id_product_attribute":null,"description":"<p><strong>Lorem Ipsum<\/strong><span>\u00a0is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<\/span><\/p>","description_short":"<p><strong>Lorem Ipsum<\/strong>\u00a0is simply dummy text of the printing and typesetting industry.<\/p>","description_guj":"<p>\u0aaa\u0ab0\u0acd\u0ab5\u0aa4\u0aae\u0abe\u0ab3\u0abe\u0aa8\u0ac0 \u0a85\u0a82\u0aa6\u0ab0 \u0aaa\u0acd\u0ab0\u0ab5\u0ac7\u0ab6\u0ac7 \u0a9b\u0ac7 \u0aa4\u0ac7\u0ab5\u0abe\u0a82 \u0aac\u0ab9\u0ac1 \u0aa5\u0acb\u0aa1\u0abe\u0a82 \u0ab8\u0acd\u0aa5\u0ab3\u0acb\u0aae\u0abe\u0a82\u0aa8\u0abe \u0a8f\u0a95 \u0ab5\u0abe\u0a9f\u0ac7 \u0a85\u0aae\u0ac7 \u0aa4\u0abf\u0aac\u0ac7\u0a9f\u0aae\u0abe\u0a82 \u0aa6\u0abe\u0a96\u0ab2 \u0aa5\u0aaf\u0abe \u0ab9\u0aa4\u0abe. \u0a86 \u0ab5\u0a96\u0aa4\u0ac7 \u0a85\u0aae\u0ac7 \u0a86\u0a97\u0ab3 \u0a95\u0ac7\u0a9f\u0ab2\u0ac7 \u0ab8\u0ac1\u0aa7\u0ac0 \u0a9c\u0a88 \u0ab6\u0a95\u0ab6\u0ac1\u0a82 \u0aa4\u0ac7\u0aa8\u0ac0 \u0a9a\u0abf\u0a82\u0aa4\u0abe, \u0a85\u0ab2\u0aac\u0aa4\u0acd\u0aa4, \u0ab9\u0ab5\u0ac7 \u0a85\u0aae\u0aa8\u0ac7 \u0aa5\u0ab5\u0abe \u0ab2\u0abe\u0a97\u0ac0. \u0ab8\u0abe\u0ab0\u0ac7 \u0aa8\u0ab8\u0ac0\u0aac\u0ac7 \u0a85\u0ab9\u0ac0\u0a82 \u0a85\u0aae\u0aa8\u0ac7 \u0a95\u0acb\u0a88 \u0a93\u0ab3\u0a96\u0aa4\u0ac1\u0a82 \u0aa8\u0ab9\u0acb\u0aa4\u0ac1\u0a82, \u0aa8\u0ac7 \u0a95\u0acb\u0a88 \u0a9d\u0ac7\u0ab0\u0ac0\u0ab2\u0abe \u0a85\u0aae\u0ab2\u0aa6\u0abe\u0ab0\u0ac7 \u0ab2\u0acb\u0a95\u0acb\u0aa8\u0ac7 \u0a85\u0aae\u0abe\u0ab0\u0abe\u0aa5\u0ac0 \u0aad\u0aa1\u0a95\u0abe\u0ab5\u0ac7\u0ab2\u0abe \u0aaa\u0aa3 \u0aa8\u0ab9\u0acb\u0aa4\u0abe. \u0a95\u0acb\u0a88 \u0aaa\u0ac2\u0a9b\u0aaa\u0ab0\u0a9b \u0a95\u0ab0\u0aa4\u0ac1\u0a82, \u0aa4\u0acb \u0a85\u0aae\u0ac7 \u0a95\u0ab9\u0ac7\u0aa4\u0abe \u0a95\u0ac7 \u0a95\u0ac8\u0ab2\u0abe\u0ab8\u0aa8\u0ac0 \u0a9c\u0abe\u0aa4\u0acd\u0ab0\u0abe\u0a8f \u0aa8\u0ac0\u0a95\u0ab3\u0acd\u0aaf\u0abe \u0a9b\u0ac0\u0a8f<\/p>","description_short_guj":"<p>\u0aaa\u0ab0\u0acd\u0ab5\u0aa4\u0aae\u0abe\u0ab3\u0abe\u0aa8\u0ac0 \u0a85\u0a82\u0aa6\u0ab0 \u0aaa\u0acd\u0ab0\u0ab5\u0ac7\u0ab6\u0ac7 \u0a9b\u0ac7 \u0aa4\u0ac7\u0ab5\u0abe\u0a82 \u0aac\u0ab9\u0ac1 \u0aa5\u0acb\u0aa1\u0abe\u0a82 \u0ab8\u0acd\u0aa5\u0ab3\u0acb\u0aae\u0abe\u0a82\u0aa8\u0abe \u0a8f\u0a95 \u0ab5\u0abe\u0a9f\u0ac7 \u0a85\
11-12 06:04:59.072: W/System.err(2748):  at 0 of type java.lang.String cannot be converted to JSONObject
11-12 06:04:59.081: W/System.err(2748):     at org.json.JSON.typeMismatch(JSON.java:100)
11-12 06:04:59.081: W/System.err(2748):     at org.json.JSONArray.getJSONObject(JSONArray.java:484)
11-12 06:04:59.081: W/System.err(2748):     at app.navbharatpublication.tablet.HomePage.onCreate(HomePage.java:549)
11-12 06:04:59.081: W/System.err(2748):     at android.app.Activity.performCreate(Activity.java:5104)
11-12 06:04:59.091: W/System.err(2748):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-12 06:04:59.091: W/System.err(2748):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
11-12 06:04:59.091: W/System.err(2748):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
11-12 06:04:59.091: W/System.err(2748):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-12 06:04:59.091: W/System.err(2748):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
11-12 06:04:59.091: W/System.err(2748):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-12 06:04:59.103: W/System.err(2748):     at android.os.Looper.loop(Looper.java:137)
11-12 06:04:59.111: W/System.err(2748):     at android.app.ActivityThread.main(ActivityThread.java:5041)
11-12 06:04:59.111: W/System.err(2748):     at java.lang.reflect.Method.invokeNative(Native Method)
11-12 06:04:59.111: W/System.err(2748):     at java.lang.reflect.Method.invoke(Method.java:511)
11-12 06:04:59.111: W/System.err(2748):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-12 06:04:59.121: W/System.err(2748):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-12 06:04:59.121: W/System.err(2748):     at dalvik.system.NativeStart.main(Native Method)
4

3 回答 3

0

那里的服务器正在返回一个

JSON数组 []

你正在尝试构建

JSON对象{}

使用 JSONArray 解析响应,然后您可以进一步查找其他错误:)

于 2013-11-12T06:54:43.620 回答
0

尝试如下:

解析

  JSONArray arr = new JSONArray(result);

    for(int i = 0; i < arr.length(); i++){
           JSONObject c = arr.getJSONObject(i);        
           Log.v("result--", c.getString("image"));

   }
于 2013-11-12T06:56:10.787 回答
0

这是不正确的:

         JSONObject json = new JSONObject();
            json.put(result, true);
            Log.i("JSON FROM URL","" +  json);

相反,您应该执行以下操作:

JSONArray json = new JSONArray(result);

之后,您继续:

for (int i = 0; i < json.length(); ++i) {
    JSONObject product = json.getJSONObject(i);
    String image = product.getString("image");
    ...
}
于 2013-11-12T06:53:03.293 回答