解析 JSON 值时出错
Error parsing JSON value
我收到错误:JSON解析器:解析数据时出错org.json.JSONException:类型java.lang.String的值成功无法转换为JSON成功时的对象=json.getBoolean(成功);
我搜索了 SO 和互联网,但我仍然不明白我做错了什么。
我的 JSON 看起来像:成功
@Override
protected Boolean doInBackground(Void... params) {
//Log.v("LoginActivity", "UserLoginTask-AsyncTask-doinBackground");
Boolean success;
String username = mUsernameView.getText().toString();
String password = mPasswordView.getText().toString();
try {
//Building Parameters
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("username", mUsernameView.getText().toString()));
postParameters.add(new BasicNameValuePair("password", mPasswordView.getText().toString()));
Log.d("request!", "starting");
//getting product details by making HTTP request
//JSONObject json = jsonParser.makeHttpRequest(LOGIN_SUCCESS_URL, "POST", postParameters);
//Error parsing data org.json.JSONException:
//Value Success of type java.lang.String cannot be converted to JSONObject
String response = jsonParser.makeHttpRequest(LOGIN_SUCCESS_URL, "POST", postParameters);
Log.v("SUCCESS", SUCCESS.toString());
// Log.v("json", json.toString()); //null
//success = json.getBoolean(SUCCESS);
//if (success == true) {
if (response.toLowerCase().contains("success")) {
// Log.d("Login Successful!", json.toString());
Toast.makeText(LogIn.this, "Login Successful!", Toast.LENGTH_LONG).show();
Intent i = new Intent(getApplicationContext(), Main.class);
finish();
startActivity(i);
//return json.getBoolean(FAILURE);
} else {
//Log.d("Login Failure!", json.getString(FAILURE));
Toast.makeText(LogIn.this, "Login Fail!", Toast.LENGTH_LONG).show();
Intent i = new Intent(getApplicationContext(), MainActivity.class);
finish();
startActivity(i);
}
} catch (JSONException e) { //error here
e.printStackTrace();
}
return null;
}
}
public String 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();
Log.v("makeHttpRequest client", httpClient.toString());
HttpPost httpPost = new HttpPost(url);
Log.v("makeHttpRequest post", httpPost.toString());
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
Log.v("makeHttpRequest is", is.toString());
}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);
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();
json = sb.toString();
Log.v("json=sb", sb.toString());
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
return json;
}
如日志:
Parser﹕ Error parsing data org.json.JSONException: Value Success of
type java.lang.String cannot be converted to JSONObject
表示不从服务器获取 JSONObject
而只是从服务器获取 Success
字符串作为响应。
因此,无需将 json
转换为 JSONObject
。进行以下更改以使其正常工作:
1. 将 makeHttpRequest
方法的 return 类型更改为 String
和 return json
(删除或评论 jObj = new JSONObject(json);
行 )
2. 在 doInBackground
中调用 makeHttpRequest
方法为:
String response = jsonParser.makeHttpRequest(LOGIN_SUCCESS_URL,
"POST",
postParameters);
if(response.toLowerCase().contains("success")){
/// do task
}else{
// do task
}
我收到错误:JSON解析器:解析数据时出错org.json.JSONException:类型java.lang.String的值成功无法转换为JSON成功时的对象=json.getBoolean(成功);
我搜索了 SO 和互联网,但我仍然不明白我做错了什么。
我的 JSON 看起来像:成功
@Override
protected Boolean doInBackground(Void... params) {
//Log.v("LoginActivity", "UserLoginTask-AsyncTask-doinBackground");
Boolean success;
String username = mUsernameView.getText().toString();
String password = mPasswordView.getText().toString();
try {
//Building Parameters
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("username", mUsernameView.getText().toString()));
postParameters.add(new BasicNameValuePair("password", mPasswordView.getText().toString()));
Log.d("request!", "starting");
//getting product details by making HTTP request
//JSONObject json = jsonParser.makeHttpRequest(LOGIN_SUCCESS_URL, "POST", postParameters);
//Error parsing data org.json.JSONException:
//Value Success of type java.lang.String cannot be converted to JSONObject
String response = jsonParser.makeHttpRequest(LOGIN_SUCCESS_URL, "POST", postParameters);
Log.v("SUCCESS", SUCCESS.toString());
// Log.v("json", json.toString()); //null
//success = json.getBoolean(SUCCESS);
//if (success == true) {
if (response.toLowerCase().contains("success")) {
// Log.d("Login Successful!", json.toString());
Toast.makeText(LogIn.this, "Login Successful!", Toast.LENGTH_LONG).show();
Intent i = new Intent(getApplicationContext(), Main.class);
finish();
startActivity(i);
//return json.getBoolean(FAILURE);
} else {
//Log.d("Login Failure!", json.getString(FAILURE));
Toast.makeText(LogIn.this, "Login Fail!", Toast.LENGTH_LONG).show();
Intent i = new Intent(getApplicationContext(), MainActivity.class);
finish();
startActivity(i);
}
} catch (JSONException e) { //error here
e.printStackTrace();
}
return null;
}
}
public String 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();
Log.v("makeHttpRequest client", httpClient.toString());
HttpPost httpPost = new HttpPost(url);
Log.v("makeHttpRequest post", httpPost.toString());
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
Log.v("makeHttpRequest is", is.toString());
}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);
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();
json = sb.toString();
Log.v("json=sb", sb.toString());
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
return json;
}
如日志:
Parser﹕ Error parsing data org.json.JSONException: Value Success of type java.lang.String cannot be converted to JSONObject
表示不从服务器获取 JSONObject
而只是从服务器获取 Success
字符串作为响应。
因此,无需将 json
转换为 JSONObject
。进行以下更改以使其正常工作:
1. 将 makeHttpRequest
方法的 return 类型更改为 String
和 return json
(删除或评论 jObj = new JSONObject(json);
行 )
2. 在 doInBackground
中调用 makeHttpRequest
方法为:
String response = jsonParser.makeHttpRequest(LOGIN_SUCCESS_URL,
"POST",
postParameters);
if(response.toLowerCase().contains("success")){
/// do task
}else{
// do task
}