【发布时间】:2016-12-26 12:21:01
【问题描述】:
我正在处理登录页面和服务器接收数据。这里的 IP、用户名和密码由用户输入。当 IP 正确时,他会检查用户名和密码。之后他会收到 JSON 消息。这里,结果保存为“用户存在”,我正在检查“用户存在”。
当我试图在JSONObject 中转换时,他给出了错误.....
here is the error description
JSON 响应:“用户存在”
MainActivity.java
private class HttpAsyncTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... urls) {
return GET(urls[0]);
}
@Override
protected void onPostExecute(String result) {
try {
JSONObject jsonResponse = new JSONObject(result);
if(jsonResponse.equals("User exists")){
Toast.makeText(getBaseContext(),"Login Successfully",Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(),Welcome.class);
startActivity(intent);
}
else{
Toast.makeText(getBaseContext(),"User does not exists",Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
LogCat
12-27 10:53:20.662 507-572/system_process E/WifiStateMachine: [1,482,836,000,662 ms] noteScanstart no scan source uid -1
12-27 10:53:21.375 6641-6662/com.example.anew.addbtnontitlebar D/result: "User exists"
12-27 10:53:21.376 6641-6641/com.example.anew.addbtnontitlebar D/error: Value User exists of type java.lang.String cannot be converted to JSONObject
12-27 10:53:23.168 507-572/system_process E/WifiStateMachine: [1,482,836,003,168 ms] noteScanEnd no scan source onTime=0
【问题讨论】:
-
无 JSONException 重复
-
试试 jsonResponse.toString()..equals("用户存在")
-
JSONObject jsonResponse = new JSONObject(result);删除此行并直接从result' likeif(result.equals("User exists"))` 中检查。如果服务器只返回此文本。 -
我不明白你能解释一下@K
标签: android json android-asynctask