2013-03-12 90 views
3

我輸入錯誤「解析數據時出錯org.json.JSONException:字符10輸入結束於」。我用Chrome的插件郵差測試了我的PHP,服務器端似乎沒問題。請查看下面的代碼並感謝您的幫助。解析數據時出錯org.json.JSONException:字符10輸入結束時

PHP代碼:

public function login($alias, $password){ 
$user_info = $this->getUserFromDatabase($alias, $password); 
if ($user_info != false){ 
$response["success"] = "true"; 
$response["user_id"] = $user_info["userID"]; 
$response["userFirstName"] = $user_info["userFirstName"]; 
$response["userRank"] = $user_info["userRank"]; 
echo json_encode($response); 
}else{ 
$response["success"] = "false"; 
$response["error"] = "true"; 
echo json_encode($response); 
} 
} 

public function getUserFromDatabase($android_alias, $android_password) { 
$db_query = mysql_query("SELECT userID, userFirstName, userRank FROM capUserTable 
WHERE userAlias = '$android_alias' AND userPassword = '$android_password'") 
or die(mysql_error()); 

$query_results = mysql_fetch_assoc($db_query); 
return $query_results; 
} 

郵差結果:

{ 
"success": "true", 
"user_id": "1", 
"userFirstName": "username", 
"userRank": "99" 
} 

Java代碼:

private InputStream inputStream = null; 
private JSONObject jObject = null; 
private String json = ""; 

public JSONparser() { 
} 

public JSONObject getJSONFromURL(String URL, List<NameValuePair> params){ 
try{ 
DefaultHttpClient httpClient = new DefaultHttpClient(); 
HttpPost httpPost = new HttpPost(URL); 
httpPost.setEntity(new UrlEncodedFormEntity(params)); 
HttpResponse httpResponse = httpClient.execute(httpPost); 
HttpEntity httpEntity = httpResponse.getEntity(); 
inputStream = httpEntity.getContent(); 
}catch(UnsupportedEncodingException e){ 
e.printStackTrace(); 
Log.e("UnsupportedEncodingException", "Unsupported Encoding Exception" + e.toString()); 
}catch(ClientProtocolException e){ 
e.printStackTrace(); 
}catch(IOException e){ 
e.printStackTrace(); 
}catch(Exception e){ 
e.printStackTrace(); 
} 

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

try{ 
jObject = new JSONObject(json); //this is where the problem occurs 
}catch(JSONException e){ 
Log.e("JSON Parser", "Error parsing data " + e.toString()); 
} 
return jObject; 
} 
+0

後堆棧跟蹤用問題 – 2013-03-12 05:01:22

+0

在android中記錄json變量的內容並檢查它。 – Josnidhin 2013-03-12 05:03:26

+0

hi josnidhin,json變量爲null。 – thienwgu 2013-03-12 05:07:24

回答

2

嗨,我有ç哎呀你的反應得到一個問題

String response = "{success\":\"true\",\"user_id\": \"1\",\"userFirstName\":\"username\", \"userRank\": \"99\"}"; 

其在JSON驗證顯示有效,但在成功的關鍵不是「開頭,所以它的創建JSON對象像

{"userFirstName":"username","user_id":"1","success\"":"true","userRank":"99"} 

所以它無法從成功

獲取值

寫東西像

String response = "{\"success\":\"true\",\"user_id\": \"1\",\"userFirstName\":\"username\", \"userRank\": \"99\"}"; 

,它應該解析這樣

  try { 
      JSONObject jobj = new JSONObject(response); 
      String succes = jobj.getString("success"); 
      String userFirstName = jobj.getString("userFirstName"); 
      String user_id = jobj.getString("user_id"); 
      String userRank = jobj.getString("userRank"); 
      String user_id = jobj.getString("user_id"); 

     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
+0

嗨ankitmakwana,我今天會嘗試,看看這是否可以解決它。謝謝您的回覆。 – thienwgu 2013-03-12 12:49:11

+0

夢幻般的ankitmakwana,這沒有把戲,謝謝你的幫助。 – thienwgu 2013-03-12 23:29:45

+0

@Ankit Makwana:你好Ankit,你能回答這個問題嗎? http://stackoverflow.com/questions/31238764/posting-base64-image-to-php-server-in-android/31238916?noredirect=1#comment50477664_31238916 – Devraj 2015-07-06 07:42:50

1

其實我也是前臉.... 我清楚通過改變我的賈森類的bug同樣的問題...

// function get json from url 
    // by making HTTP POST or GET mehtod 
    public JSONObject makeHttpRequest(String url, String method, 
        List<NameValuePair> params) { 

      // Making HTTP request 
      try { 

        // check for request method 
        if(method == "POST"){ 
          // request method is POST 
          // defaultHttpClient 
          DefaultHttpClient httpClient = new DefaultHttpClient(); 
          HttpPost httpPost = new HttpPost(url); 
          httpPost.setEntity(new UrlEncodedFormEntity(params)); 

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

        }else if(method == "GET"){ 

          // request method is GET 
          DefaultHttpClient httpClient = new DefaultHttpClient(); 
          String paramString = URLEncodedUtils.format(params, "utf-8"); 
          url += "?" + paramString; 
          HttpGet httpGet = new HttpGet(url); 

          HttpResponse httpResponse = httpClient.execute(httpGet); 
          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()); 
      } 

      // try parse the string to a JSON object 
      try { 
       Log.d("response string",json); 
        jObj = new JSONObject(json); 
      } catch (JSONException e) { 
        Log.e("JSON Parser", "Error parsing data " + e.toString()); 
      } 

      // return JSON String 
      return jObj; 

    } 

我希望這將有助於ü

+0

我的類是非常相似的,只有我得到IOException錯誤因此,我也得到一個BufferError和一個JSONParsing錯誤!爲什麼? – Pheonix7 2013-12-04 09:42:54

相關問題