2014-05-12 79 views
-1

我想從服務器檢索一行。從json檢索給出了錯誤android

我收到以下錯誤。

服務器上的日誌

E/log_tag(1948): connection success [sendernumber=9040307070] 
E/log_tag(1948): result ?9545357470[{"TaskId":"1"},{"TaskId":"2"}] 
E/log_tag(1948): Error parsing data org.json.JSONException: Value 9040307070 of type java.lang.Long cannot be converted to JSONArray 

JSON代碼

 String result = null; 
    InputStream is = null; 
    try{ 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://www.mydomainname.org/task/senttask.php"); 
      ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 

      nameValuePairs.add(new BasicNameValuePair("sendernumber",name.toString())); 
      //nameValuePairs.add(new BasicNameValuePair("sendernumber","9595959595")); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      HttpResponse response = httpclient.execute(httppost); 

      HttpEntity entity = response.getEntity(); 
      is = entity.getContent(); 

      Log.e("log_tag", "connection success "+nameValuePairs); 

    } 
    catch(Exception e) 
    { 
      Log.e("log_tag", "Error in http connection "+e.toString()); 
      Toast.makeText(getActivity(), "Connection fail", Toast.LENGTH_SHORT).show(); 

    } 
    try 
    { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(is,HTTP.UTF_8),8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) 
      { 
        sb.append(line + "\n"); 

      } 
      is.close(); 

      result=sb.toString(); 
      Log.e("log_tag", "result "+result.toString()); 
    } 
    catch(Exception e) 
    { 
      Log.e("log_tag", "Error converting result "+e.toString()); 
     Toast.makeText(getActivity(), " Input reading fail", Toast.LENGTH_SHORT).show(); 

    } 
    try 
    { 
     JSONArray jArray = new JSONArray(result); 
      String s="",s1,s2,s3,s4,s5; 
      Log.w("Lengh",""+jArray.length()); 
      for(int i=0;i<jArray.length();i++){ 

       JSONObject json_data = jArray.getJSONObject(i); 

        s=json_data.getString("TaskId"); 

        Log.e("taskid from server","s"); 

      } 



    } 
    catch(JSONException e) 
    { 
      Log.e("log_tag", "Error parsing data "+e.toString()); 
      Toast.makeText(getActivity(), "JsonArray fail", Toast.LENGTH_SHORT).show(); 
    } 

TaskId int primary key 
sendernumber varchar(100) 
+2

看來你沒有得到響應json數組正確!請檢查併發布你正在收到什麼回覆 –

+0

我只得到很多錯誤日誌 – user3256145

+0

你得到的迴應9545357470 [{「TaskId」:「1」},{「TaskId」:「2」}]爲什麼數據9545357470附有你的迴應?你是否爲此進行過調查? –

回答

1

如果你想將它WO rk,用JSONArray jArray = new JSONArray(result.substring(result.indexOf("[")));代替JSONArray jArray = new JSONArray(result);

然而,你的web服務似乎有些問題。它返回9545357470[{"TaskId":"1"},{"TaskId":"2"}],那真的應該是[{"TaskId":"1"},{"TaskId":"2"}]。所以我不確定當你試圖創建你的json數組來發送它作爲響應時發生了什麼,但最好的解決方案是解決這個問題,而不是你在這裏的代碼。

相關問題