gpt4 book ai didi

java - volley get 响应返回 java.lang.String 类型的值无法转换为 JSONArray

转载 作者:行者123 更新时间:2023-12-01 18:29:16 25 4
gpt4 key购买 nike

我正在尝试使用 api 进行身份验证。我收到响应错误,我的 api 返回以下响应并收到以下错误。

value of type java.lang.String cannot be converted to JSONArray.

请帮忙,我是新手。

这是我的 api 返回的内容:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
[{"status":"True","username":"parent","schoolid":"001","category":"3","usercode":"0720994718"}]
</string>

这是我的代码;

RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
//create a StringRequest
StringRequest request = new StringRequest( Request.Method.GET,
URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {

JSONArray jArray = new JSONArray(response);

int i = 0;
while (i < jArray.length()) {

JSONObject json_data = jArray.getJSONObject(i);

String status = json_data.getString("status");
if (status == "True") {
Intent intent = new Intent(LoginActivity.this, ParentActivity.class);
startActivity(intent);
Toast.makeText(LoginActivity.this, "logged in as " + user,
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(LoginActivity.this, "wrong username or password",
Toast.LENGTH_LONG).show();
}
i++;

}
} catch (JSONException e) {
e.printStackTrace();
}
}

},

new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(LoginActivity.this, "Error!!! " + error.toString(),
Toast.LENGTH_LONG).show();
}
}
) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("username", user);
params.put("password", pass);
return params;
}
};
//add the StringRequest to the RequestQueue
queue.add(request);
}

最佳答案

我将 JSONArray jArray = new JSONArray(response); 更改为 JSONObject jObject = new JSONObject(response);JSONObject json_data = jArray.getJSONObject( i);String json_data = jObject(i);。请尝试一下。

RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
//create a StringRequest
StringRequest request = new StringRequest( Request.Method.GET,
URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {

JSONObject jObject = new JSONObject(response);

int i = 0;
while (i < jArray.length()) {

String json_data = jObject(i);

String status = json_data.getString("status");
if (status == "True") {
Intent intent = new Intent(LoginActivity.this, ParentActivity.class);
startActivity(intent);
Toast.makeText(LoginActivity.this, "logged in as " + user,
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(LoginActivity.this, "wrong username or password",
Toast.LENGTH_LONG).show();
}
i++;

}
} catch (JSONException e) {
e.printStackTrace();
}
}

},

new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(LoginActivity.this, "Error!!! " + error.toString(),
Toast.LENGTH_LONG).show();
}
}
) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("username", user);
params.put("password", pass);
return params;
}
};
//add the StringRequest to the RequestQueue
queue.add(request);
}

关于java - volley get 响应返回 java.lang.String 类型的值无法转换为 JSONArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60189062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com