我想決議 JSON 回應。我無法決議回應。它顯示org.json.JSONException: No value for String Response此錯誤。
這是我的代碼`public void onResponse(JSONObject response) {
Log.d("TAG", "Details:" response);
responseTV.setText("String Response : " response.toString());
try {
JSONObject jsonObject = response.getJSONObject("String Response" response);
strcode = jsonObject.getString("responseCode");
strtext = jsonObject.getString("responseText");
strname = jsonObject.getString("personName");
Log.i("TAG","parseData:" strname);
response_code.setText("" strcode);
response_text.setText("" strtext);
person_name.setText("" strname);
} catch (JSONException e) {
Log.d("TAG", "profile: " e);
}
}`
uj5u.com熱心網友回復:
我猜您使用 Volley JsonObjectRequest,因此您需要將回應轉換為 String 并將其決議為 a JSONObject,如下所示:
jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
response -> {
try {
JSONObject jsonObject = new JSONObject(response.toString());
strcode = jsonObject.getString("responseCode");
strtext = jsonObject.getString("responseText");
strname = jsonObject.getString("personName");
Log.i("TAG","parseData:" strname);
response_code.setText("" strcode);
response_text.setText("" strtext);
person_name.setText("" strname);
} catch (JSONException e) {
Log.d("TAG", "profile: " e);
}
}, error -> {}
);
uj5u.com熱心網友回復:
try {
JSONObject jsonObject = response.getJSONObject(response);
strcode = jsonObject.getString("responseCode");
strtext = jsonObject.getString("responseText");
strname = jsonObject.getString("personName");
Log.i("TAG","parseData:" strname);
response_code.setText("" strcode);
response_text.setText("" strtext);
person_name.setText("" strname);
} catch (JSONException e) {
Log.d("TAG", "profile: " e);
}
在進行決議之前,您將通過 API exp 檢查所有接收值:- responseCode、responseText、personName ( https://jsonlint.com/ ),如果您接收到所有值,請檢查其格式是否正確,然后您將決議資料
你也用這個,它會處理 JsonException
jsonObject.optString("responseText");
uj5u.com熱心網友回復:
用這段代碼解決了這個問題
public void onResponse(JSONObject response) {
Log.d("TAG", "Details:" response);
responseTV.setText("String Response : " response.toString());
try {
// JSONObject jsonObject = response.getJSONObject(response.toString());
strcode = response.getString("responseCode");
strtext = response.getString("responseText");
strname = response.getString("personName");
Log.i("TAGParser","parseData:" strname);
response_code.setText("" strcode);
response_text.setText("" strtext);
person_name.setText("" strname);
} catch (JSONException e) {
Log.d("TAG", "profile: " e);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/448563.html
上一篇:單擊復選框時未保存Cookie
