org.json误差解析数据。JSONException:在字符10处的输入结束

时间:2022-10-29 14:19:21

I am currently stuck at this error "Error parsing data org.json.JSONException: End of input at character 10 of". I tested my PHP with Chrome's Plugin Postman and the server-side seems to be fine. Please view code below and thank you for your help.

我目前被这个错误“错误解析数据org.json”困住了。JSONException:在“”字符10处的输入结束。我用Chrome的插件Postman测试了我的PHP,服务器端似乎没问题。请查看下面的代码并感谢您的帮助。

PHP code:

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

Postman results:

邮差结果:

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

JAVA code:

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

2 个解决方案

#1


2  

Hi i have check your response get one issue

你好,我查了你的回复,有一个问题

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

its in json validation display valid but in success key not " in starting so its create json object like

它在json验证中显示有效,但在成功键中没有“在开始时,它创建的json对象类似”

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

so its could not get values from success

所以它不能从成功中获得价值

Write things like

写之类的东西

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

and it should parse like this

它应该像这样解析

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

#2


1  

actually me too face the same problem before .... i clear the bug by changing my jason class...

其实我也是在....之前面临同样的问题我通过改变我的杰森课程来清除错误……

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

  } 

i hope it will helps u

我希望它能帮助你。

#1


2  

Hi i have check your response get one issue

你好,我查了你的回复,有一个问题

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

its in json validation display valid but in success key not " in starting so its create json object like

它在json验证中显示有效,但在成功键中没有“在开始时,它创建的json对象类似”

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

so its could not get values from success

所以它不能从成功中获得价值

Write things like

写之类的东西

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

and it should parse like this

它应该像这样解析

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

#2


1  

actually me too face the same problem before .... i clear the bug by changing my jason class...

其实我也是在....之前面临同样的问题我通过改变我的杰森课程来清除错误……

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

  } 

i hope it will helps u

我希望它能帮助你。