【发布时间】:2025-12-02 13:00:02
【问题描述】:
我需要一些帮助。每当我实施时:
JSONArray jsonArray = new JSONArray(response);
它会导致我的代码跳转到异常块。
我用的是php的方法:
echo json_encode(array("code"=>$code));
将 JSON 字符串传递给我的应用程序。我得到的原始响应字符串是:
{"code":"UserIsValid"}
我查看了文档,它说JSONArray(String json) 是初始化 JSON 数组的有效方法。我正在使用 Volley 与我的数据库进行通信,这似乎运行良好。我的相关应用程序代码如下所示:
StringRequest postRequest = new StringRequest(Request.Method.POST, server_url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// response
try {
JSONArray jsonArray = new JSONArray(response);
Toast.makeText(RegisterActivity.this, response, Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(RegisterActivity.this, "failed", Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// error response
Toast.makeText(RegisterActivity.this, "Server file not found", Toast.LENGTH_SHORT).show();
}
}
) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("name", user_name);
return params;
}
};
//Adds post request to request queue using MySingleton class.
MySingleton.getInstance(RegisterActivity.this).addToRequestQueue(postRequest);
我很高兴得到任何帮助!
【问题讨论】:
标签: php android json android-studio android-volley