Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中,JSONArray无法转换为JSONObject_Android_Node.js_Rest - Fatal编程技术网

在Android中,JSONArray无法转换为JSONObject

在Android中,JSONArray无法转换为JSONObject,android,node.js,rest,Android,Node.js,Rest,我的Android应用程序向REST API发送请求以验证用户身份。服务器在该请求中返回true。但在我的应用程序中,运行到onErrorResponse(),而没有运行到onResponse()。我打印的错误是: com.android.volley.ParseError:org.json.JSONException:Value[{“id”:1,“username”:“vuthehuyht”,“full_name”:“VũTh”ế org.json.JSONArray类型的Huy、“电话号码”

我的Android应用程序向REST API发送请求以验证用户身份。服务器在该请求中返回true。但在我的应用程序中,运行到
onErrorResponse()
,而没有运行到
onResponse()
。我打印的错误是:

com.android.volley.ParseError:org.json.JSONException:Value[{“id”:1,“username”:“vuthehuyht”,“full_name”:“VũTh”ế org.json.JSONArray类型的Huy、“电话号码”:“0972809817”、“密码”:“hoanglan”}]无法转换为JSONObject

验证器函数
如何修复它?

只需修改您的截击请求
onResponse
回调,以便接受
JSONArray
作为响应,而不是
JSONObject
,因为这是您从服务器获得的响应类型:

@Override
public void onResponse(JSONArray response) {
     loadingBar.dismiss();
     Toast.makeText(LoginActivity.this, "Login successfully", Toast.LENGTH_SHORT).show();
}

如果这是你的回答

[ 
  {
    "id":1,
    "username":"vuthehuyht",
    "full_name":"Vũ Thế Huy",
    "phone_number":"0972809817",
    "password":"hoanglan"
  }
]
您可能需要从JSONArray开始,比如

试试这个方法

RequestQueue RequestQueue=Volley.newRequestQueue(getApplicationContext());
StringRequest StringRequest=newStringRequest(Request.Method.POST,“Url”,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
字符串错误=”;
试一试{
JSONArray JSONArray=新的JSONArray();

对于(int i=0;iIs您的服务器就是您将请求发送到的服务器?如果是,请将响应的格式更改为包含JSONArray的JSONObject。在截取中还有一个
JsonArrayRequest
,因此可能应该使用它而不是
JsonObjectRequest
,因为您在请求中显然收到了一个JSON数组有
电话号码
密码
字段。使用
JsonArrayRequest
JsonObjectRequest
@Override
public void onResponse(JSONArray response) {
     loadingBar.dismiss();
     Toast.makeText(LoginActivity.this, "Login successfully", Toast.LENGTH_SHORT).show();
}
[ 
  {
    "id":1,
    "username":"vuthehuyht",
    "full_name":"Vũ Thế Huy",
    "phone_number":"0972809817",
    "password":"hoanglan"
  }
]