我已經使用凌雲實現了一個自定義請求,以便登錄到網絡服務器。Android凌空自定義請求
這是使用功能的IM:
private void volleyTest2(String username, String password, String deviceid) {
Map<String, String> params;
params = new HashMap<>();
params.put("username", username);
params.put("password", password);
//params.put("device_id", deviceid);
params.put("device_id", "H767A76S7D6D");
Volley.newRequestQueue(MainActivity.this).add(
new CustomJsonRequest(Request.Method.POST, URL, params,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
String output="";
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
output+=obj.getString("name") + " "+ obj.getString("surname");
} catch (JSONException e) {
e.printStackTrace();
}
}
Toast.makeText(getApplicationContext(), "Benvenuto, " + output, Toast.LENGTH_LONG).show();
showProgress(false);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Error: " + error.getMessage());
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
showProgress(false);
}
}
)
{
@Override
protected Response parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONArray(jsonString), HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
}
);
}
這是我CustomJsonRequest:
public class CustomJsonRequest extends Request {
Map<String, String> params;
private Response.Listener listener;
public CustomJsonRequest(int requestMethod, String url, Map<String, String> params, Response.Listener responseListener, Response.ErrorListener errorListener) {
super(requestMethod, url, errorListener);
this.params = params;
this.listener = responseListener;
}
@Override
protected void deliverResponse(Object response) {
listener.onResponse(response);
}
@Override
public Map<String, String> getParams() throws AuthFailureError {
return params;
}
@Override
protected Response parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONObject(jsonString), HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
}
因爲我不能編輯PHP端是這樣的情景: - 如果登錄是正確的,我收到一個JSONArray - 如果登錄不正確,我收到一個像這樣的JSONObject: 我唯一的問題是,如果登錄不正確,我收到此錯誤: Error: org.json.JSONException: Value {"error":"Username\/password non trovati"} of type org.json.JSONObject cannot be converted to JSONArray
那麼,如何處理不正確的登錄,如果我得到一個JSONObject而不是JSONArray的? 我想只顯示對象裏面的內容,所以:"Username\/password non trovati"
UPDATE1:
private void volleyTest2(String username, String password, String deviceid) {
Map<String, String> params;
params = new HashMap<>();
params.put("username", username);
params.put("password", password);
//params.put("device_id", deviceid);
params.put("device_id", "H767A76S7D6D");
Volley.newRequestQueue(MainActivity.this).add(
new CustomJsonRequest(Request.Method.POST, URL, params,
new Response.Listener<JsonElement>() {
@Override
public void onResponse(JsonElement element) {
String output="";
// Parsing json
if(element.isJsonArray()){
JsonArray array=element.getAsJsonArray();
//read response here
Log.d(TAG,"array.toString(): " + array.toString());
}else if(element.isJsonObject()){
JsonObject object=element.getAsJsonObject();
//read response here
Log.d(TAG,"object.toString(): " + object.toString());
}
Toast.makeText(getApplicationContext(), "Benvenuto, " + output, Toast.LENGTH_LONG).show();
showProgress(false);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Error: " + error.getMessage());
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
showProgress(false);
}
}
)
{
@Override
protected Response parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONArray(jsonString), HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
}
);
}
Dem,仍然一樣。 它是正確的,它有效,otherwyse,無論如何返回這個字符串: 錯誤:org.json.JSONException:值{「錯誤」:「用戶名\ /密碼非trovati」}類型org.json.JSONObject無法轉換爲JSONArray – Vesco
你試過了哪一個? – Bharatesh
其工作與否... **:)** – Bharatesh