我正在研究android项目和我的网络服务成功地在邮递员中给出响应,因为我在下面提到了回复。从我的API获得响应后,我收到以下错误。我不是第一次做这种类型的Web服务调用,但不知道为什么会发生这种情况。我怎样才能做到这一点?
邮递员的网络服务响应 -
[
{
"emp_id": 43065,
"emp_name": "Rahul Bhandari",
"username": "43065",
"password": null
}
]
错误 -
org.json.JSONException: Value [{"emp_id":43065,"emp_name":"Rahul Bhandari","username":"43065","password":null}] of type org.json.JSONArray cannot be converted to JSONObject
排球码 -
public void apiCall(final String email, final String password) {
processArray = new ArrayList<String>();
HashMap<String, String> params = new HashMap<String, String>();
params.put("username", email);
params.put("password", password);
JsonObjectRequest request_json = new JsonObjectRequest(AppConfig.login, new JSONObject(params),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if (mStatusCode == 200) {
try {
loginResult = new JSONArray(response.toString());
try {
if (loginResult != null) {
for (int i = 0; i < loginResult.length(); i++) {
JSONObject obj = loginResult.getJSONObject(i);
// SQLite database handler
db = new SQLiteHandler(LoginActivity.this);
// Session manager
session = new SessionManager(LoginActivity.this);
session.setLogin(true);
// Inserting row in users table
db.addUser(obj.getString("username"), obj.getString("emp_name"), obj.getString("emp_id"), obj.getString("emp_designation"), obj.getString("emp_location"));
Intent intent = new Intent(LoginActivity.this, SelectAuditActivity.class);
startActivity(intent);
finish();
Toast.makeText(LoginActivity.this, "Successfull login...", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(LoginActivity.this, "Invalid login credential...", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
} catch (JSONException e) {
e.printStackTrace();
}
//Process os success response
} else {
Toast.makeText(LoginActivity.this, "Oops sorry something went wrong...", Toast.LENGTH_SHORT).show();
}
//Process os success response
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error: ", error.getMessage());
}
}) {
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
mStatusCode = response.statusCode;
return super.parseNetworkResponse(response);
}
};
;
// add the request object to the queue to be executed
AppController.getInstance().addToRequestQueue(request_json);
}
答案 0 :(得分:1)
您的回复是json数组
[
{
"emp_id": 43065,
"emp_name": "Rahul Bhandari",
"username": "43065",
"password": null
}
]
但您正在制作JsonObjectRequest
,请改为JsonArrayRequest
修改强>
我看到你帖子的身体是一个Json对象,你不能直接在JsonArrayRequest
发送一个Json对象,所以你必须使用getBody()
方法发送身体
示例请求
JsonArrayRequest jsObjRequest = new JsonArrayRequest
(requestMethod, url, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//do some thing with response
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// handelErrorResponse
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return setHeaders();
}
@Override
public byte[] getBody() {
String body;
// convert your json object to string and send it
body= yourJsonobject.toString();
return body.getBytes();
}
@Override
protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
getHeader(response);
return super.parseNetworkResponse(response);
}
};
addToRequestQueue(jsObjRequest);
答案 1 :(得分:1)
JsonArrayRequest request_json = new JsonArrayRequest(AppConfig.login, new JSONArray(params),
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
我认为你必须用JsonArrayRequest替换JsonObjectRequest一旦尝试替换它们,就像上面我没试过这样,但希望它可以帮助你。
答案 2 :(得分:0)
这些方括号[ {...}, {...},.. ]
代表JSONArray。因此,如果您的数组只有一个项目,请将JsonObjectRequest
更改为JsonArrayRequest
,这将返回JSONArray,然后从此数组中获取第一项。
JsonArrayRequest req = new JsonArrayRequest(urlJsonArry,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
JSONObject result = response.get(0);
}
}
答案 3 :(得分:0)
您正在使用对象JsonObjectRequest
发出请求,这意味着您的请求期望对象格式为json,如:
{ a: "a", b: "b" }
但您的服务正在响应一个类似于:
的json数组[{a:"a", b:"b"} , {a:"a1", b:"b1"}]
因此,为了摆脱您的错误,请使用JsonArrayRequest
更改您的请求的方式
答案 4 :(得分:0)
使用此代码
if (loginResult != null) {
//for (int i = 0; i < loginResult.length(); i++) {
JSONObject obj = loginResult.getJSONObject(0);
//JSONObject obj = loginResult.getJSONObject(i);
// SQLite database handler
db = new SQLiteHandler(LoginActivity.this);
// Session manager
session = new SessionManager(LoginActivity.this);
session.setLogin(true);
// Inserting row in users table
/*db.addUser(obj.getString("username"), obj.getString("emp_name"), obj.getString("emp_id"), obj.getString("emp_designation"), obj.getString("emp_location"));*/
db.addUser(obj.getString("username"), obj.getString("emp_name"), obj.getString("emp_id"),"","");
Intent intent = new Intent(LoginActivity.this, SelectAuditActivity.class);
startActivity(intent);
finish();
Toast.makeText(LoginActivity.this, "Successfull login...",
Toast.LENGTH_SHORT).show();
//}
} else {
Toast.makeText(LoginActivity.this, "Invalid login credential...", Toast.LENGTH_SHORT).show();
}
答案 5 :(得分:-1)
仅从网络服务
返回此内容{
"emp_id": 43065,
"emp_name": "Rahul Bhandari",
"username": "43065",
"password": null
}