아래 응답을 언급 할 때 안드로이드 프로젝트와 내 웹 서비스가 우편 배달부에서 성공적으로 응답하고 있습니다. API에서 응답을 얻은 후에 오류가 발생합니다. 처음으로 이러한 유형의 웹 서비스 호출을 수행하지는 않지만 왜 이런 일이 발생하는지 알지 못합니다. 이것을 어떻게 할 수 있습니까? 우편 배달부의android jsonarray가 jsonobject 오류로 변환 될 수 없습니다.
웹 서비스 응답 -
[
{
"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);
}
onResponse()는 JsonObject를 수락하지만 웹 서비스에서 json array를 수락합니다. –
게시물을 작성하기 전에 https://stackoverflow.com/help/how-to-answer를 읽어보십시오. – Kuls