[英]onResponse(String response) try { JSONObject jsonObject=new JSONObject(response);java.lang.String cannot be converted to JSONObject
我找不到答案。 嘗試了很多網站,包括這個。 我想知道問題出在哪里。
錯誤是:org.json.JSONException: 類型 java.lang.String 的值連接無法轉換為 JSONObject
這是我的安卓代碼:-
StringRequest stringRequest=new StringRequest(Request.Method.POST, URL_LOGIN,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject=new JSONObject(response);
String success=jsonObject.getString("success");
JSONArray jsonArray=jsonObject.getJSONArray("login");
if (success.equals("1")){
for (int i=0;i<jsonArray.length();i++) {
JSONObject object=jsonArray.getJSONObject(i);
String uname=object.getString("username").trim();
String email=object.getString("email");
Toast.makeText(MainActivity.this, "Login Successful!!! \n Your Name: "+uname+"\nYour Email: "+email, Toast.LENGTH_SHORT).show();
loading.setVisibility(View.GONE);
}
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this," Error!!"+e.toString(),Toast.LENGTH_LONG).show();
loading.setVisibility(View.GONE);
signin_btn.setVisibility(View.VISIBLE);
}
}
},
邏輯貓
2019-09-06 19:40:37.264 7944-7944/com.example.project W/System.err: org.json.JSONException: 類型 java.lang.String 的值連接無法轉換為 JSONObject 2019-09-06 19:40:37.264 7944-7944/com.example.project W/System.err:在 org.json.JSON.typeMismatch(JSON.java:111) 2019-09-06 19:40:37.264 7944-7944/com .example.project W/System.err: at org.json.JSONObject.(JSONObject.java:160) 2019-09-06 19:40:37.264 7944-7944/com.example.project W/System.err: at org.json.JSONObject.(JSONObject.java:173)
正如異常所暗示的那樣, response
String 不能轉換為JSONObject
:
public JSONObject(java.lang.String source) throws JSONException
拋出:
JSONException
- 如果源字符串中存在語法錯誤或重復的鍵。
來自: http : //stleary.github.io/JSON-java/org/json/JSONObject.html#JSONObject-java.lang.String-
確保您的response
字符串(從對URL_LOGIN
的 POST 請求URL_LOGIN
)確實是有效的 JSON 字符串。 要么編寫包含托管URL_LOGIN
的系統的端到端測試,要么使用 Postman 等客戶端手動測試服務URL_LOGIN
的系統URL_LOGIN
按照應用程序的請求工作。
您的 Retrofit 回調響應偵聽器將是 Retrofit 的響應類型。 使您的代碼像這樣,它會正常工作。
StringRequest stringRequest=new StringRequest(Request.Method.POST, URL_LOGIN,
new Response.Listener<Response>() {
@Override
public void onResponse(Response response) {
try {
JSONObject jsonObject=new JSONObject(response.body());
String success=jsonObject.getString("success");
JSONArray jsonArray=jsonObject.getJSONArray("login");
if (success.equals("1")){
for (int i=0;i<jsonArray.length();i++) {
JSONObject object=jsonArray.getJSONObject(i);
String uname=object.getString("username").trim();
String email=object.getString("email");
Toast.makeText(MainActivity.this, "Login Successful!!! \n Your Name: "+uname+"\nYour Email: "+email, Toast.LENGTH_SHORT).show();
loading.setVisibility(View.GONE);
}
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this," Error!!"+e.toString(),Toast.LENGTH_LONG).show();
loading.setVisibility(View.GONE);
signin_btn.setVisibility(View.VISIBLE);
}
}
},
升
當我使用這些語句時,我的代碼沒有顯示問題
JSONObject js=new JSONObject(response);
final String res = js.getString("result");
但是當我不使用它時,它工作正常:
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
HttpResponse httpResponse = httpClient.execute(httpPost);
final String response = EntityUtils.toString(httpResponse.getEntity());
JSONObject js=new JSONObject(response);
final String res = js.getString("result");
JSONObject js=new JSONObject(response);
final String res = js.getString("result");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Problem", Toast.LENGTH_SHORT).show();
}
});
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.