2013-07-30 107 views
0

我有這段代碼並嘗試運行,但它給了我例外的「org.json.JSONException:值爲<類型的java .lang.String不能轉換爲JSONObject「。任何幫助都會受到讚揚。org.json.JSONException:值<類型java.lang.String不能轉換爲JSONObject

代碼:

private class MyTask extends AsyncTask<String, Integer, String> { 

      @Override 
      protected void onPostExecute(String result) { 

       CreateAndAppendListLayout(); 

       super.onPostExecute(result); 
      } 

      @Override 
      protected void onPreExecute() { 
       // TODO Auto-generated method stub 




       super.onPreExecute(); 
      } 

      @Override 
      protected void onProgressUpdate(Integer... values) { 
       // TODO Auto-generated method stub 
       super.onProgressUpdate(values); 

      } 

      @Override 
      protected String doInBackground(String... params) { 


       String myRespone = null; 
       String url = params[0]; 
       HttpClient client = new DefaultHttpClient(); 

       HttpGet Get = new HttpGet(url); 

       try { 
        HttpResponse response = client.execute(Get); 

        HttpEntity entity = response.getEntity(); 
        myRespone = EntityUtils.toString(entity); 

       } catch (ClientProtocolException e) { 

        e.printStackTrace(); 

        Log.e("My webservice Response", "ClientProtocolException"); 

       } catch (IOException e) { 

        Log.e("My webservice Response", "IOException"); 

        e.printStackTrace(); 
       } 
       JSONObject jsonObj; 

       if (myRespone != null) { 
        try { 



         jsonObj = new JSONObject(myRespone); 
         JSONArray jsonArray = jsonObj.getJSONArray("Results"); 


         for (int i = 0; i <= jsonArray.length() - 1; i++) { 
          myList.add(jsonArray.getJSONObject(i)); 

         } 



        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
       } else { 
        // do nothing 
       } 
       return null; 
      } 

     } 

我的服務器上的JSON:

{ 
    "Results": [ 
    { 
     "id": "10", 
     "CID": "60607", 
     "userName": "Jones", 
     "userr": "hard work", 
     "st": "4" 
    }, 
    { 
     "id": "8", 
     "CID": "60807", 
     "userName": "Aaali", 
     "userr": "Good Service ", 
     "st": "5" 
    }, 
    { 
     "id": "9", 
     "CID": "65507", 
     "userName": "Jan", 
     "userr": "Happy, Cost effective ", 
     "st": "5" 
    } 
    ] 
} 

logcat的:

07-30 11:15:24.570: W/System.err(8247): org.json.JSONException: Value < of type java.lang.String cannot be converted to JSONObject 
07-30 11:15:24.570: W/System.err(8247):  at org.json.JSON.typeMismatch(JSON.java:111) 
07-30 11:15:24.570: W/System.err(8247):  at org.json.JSONObject.<init>(JSONObject.java:158) 
07-30 11:15:24.570: W/System.err(8247):  at org.json.JSONObject.<init>(JSONObject.java:171) 
07-30 11:15:24.570: W/System.err(8247):  at com.example.myclass$MyTask.doInBackground(User_Reviews.java:484) 
07-30 11:15:24.570: W/System.err(8247):  at com.example.myclass$MyTask.doInBackground(User_Reviews.java:1) 
07-30 11:15:24.570: W/System.err(8247):  at android.os.AsyncTask$2.call(AsyncTask.java:287) 
07-30 11:15:24.570: W/System.err(8247):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
07-30 11:15:24.570: W/System.err(8247):  at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
07-30 11:15:24.575: W/System.err(8247):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
07-30 11:15:24.575: W/System.err(8247):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
07-30 11:15:24.575: W/System.err(8247):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
07-30 11:15:24.575: W/System.err(8247):  at java.lang.Thread.run(Thread.java:856) 
+1

只是檢查如果字符串有任何特殊字符看不見等... – NINCOMPOOP

+1

你JSON數據是非常直接forward.I相信它包含特殊characters.Check你的對於validity.Use JSON數據這個環節 http://jsonlint.com/ –

回答

0

你試圖解析可能是JSON字符串無效它有一些raw HTML(< like),這意味着它或者生成不好,或者PHP插入errors/warnings並破壞字符串

1

檢查服務響應代碼並僅在其爲200時解析JSON否則可能是錯誤。 在你的情況下,它發送HTML代碼,因此它是崩潰

相關問題