com.android.volley.ClientError 在片段中使用 Volley 时出错
com.android.volley.ClientError error when using Volley in a fragment
我在使用 volley
的 android 项目中使用 API 时遇到困难
这个 API returns 并且在使用邮递员调用时完美运行
请告诉我如何使用 android 片段中的 API
看到下面的错误。我得到
com.android.volley.ClientError
查看下面我的代码
private void volleyApiCall(final String theEmail, final String theToken) {
// creating a new variable for our request queue
RequestQueue queue = Volley.newRequestQueue(getActivity());
// on below line we are calling a string
// request method to post the data to our API
// in this we are calling a post method.
StringRequest request = new StringRequest(Request.Method.POST, URL, new com.android.volley.Response.Listener<String>() {
@Override
public void onResponse(String response) {
// inside on response method we are
// hiding our progress bar
// and setting data to edit text as empty
// on below line we are displaying a success toast message.
Log.d(TAG, "onResponse: INSIDE RESPONSE");
Toast.makeText(getActivity(), "Data added to API", Toast.LENGTH_SHORT).show();
try {
// on below line we are passing our response
// to json object to extract data from it.
JSONObject jsonObject = new JSONObject(response);
Log.d(TAG, "onResponse: >> SUCCESS SUCCESS");
Log.d(TAG, "onResponse: jsonObject >> " + jsonObject);
String status = (jsonObject.getString("status"));
Log.d(TAG, "onResponse: status >> " + status);
String output = (jsonObject.getString("output"));
Log.d(TAG, "onResponse: output >> " + output);
String message = (jsonObject.getString("message"));
Log.d(TAG, "onResponse: message >> " + message);
if (status.equals("1")) {
String balance = fetchBalance(email, token);
tvBalance.setText(balance);
} else {
Toast.makeText(getActivity(),
"Unable to check wallet. Please Try again", Toast.LENGTH_LONG).show();
}
// on below line we are setting this string s to our text view.
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// method to handle errors.
Toast.makeText(getActivity(), "Failed to get response = " + error, Toast.LENGTH_SHORT).show();
Log.d(TAG, "onErrorResponse: ERROR IS : >> "+ error.getMessage());
Log.d(TAG, "onErrorResponse: ERROR IS : >> "+ error.getStackTrace());
Log.d(TAG, "onErrorResponse: ERROR IS : >> "+ error.getLocalizedMessage());
Log.d(TAG, "onErrorResponse: ERROR IS : >> "+ error);
}
}) {
@Override
protected Map<String, String> getParams() {
// below line we are creating a map for
// storing our values in key and value pair.
Map<String, String> params = new HashMap<String, String>();
params.put("email", theEmail);
params.put("token", theToken);
// at last we are
// returning our params.
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json");
params.put("Accept", "application/json");
return params;
}
};
request.setRetryPolicy(new RetryPolicy() {
@Override
public int getCurrentTimeout() {
return 30000;
}
@Override
public int getCurrentRetryCount() {
return 30000;
}
@Override
public void retry(VolleyError error) throws VolleyError {
}
});
// below line is to make
// a json object request.
queue.add(request);
}
--------编辑-----
我添加了邮递员的截图
谢谢大家,问题是我的后端工程师传递的响应代码一旦更改为 201 截取开始接收响应
我在使用 volley
的 android 项目中使用 API 时遇到困难这个 API returns 并且在使用邮递员调用时完美运行
请告诉我如何使用 android 片段中的 API
看到下面的错误。我得到
com.android.volley.ClientError
查看下面我的代码
private void volleyApiCall(final String theEmail, final String theToken) {
// creating a new variable for our request queue
RequestQueue queue = Volley.newRequestQueue(getActivity());
// on below line we are calling a string
// request method to post the data to our API
// in this we are calling a post method.
StringRequest request = new StringRequest(Request.Method.POST, URL, new com.android.volley.Response.Listener<String>() {
@Override
public void onResponse(String response) {
// inside on response method we are
// hiding our progress bar
// and setting data to edit text as empty
// on below line we are displaying a success toast message.
Log.d(TAG, "onResponse: INSIDE RESPONSE");
Toast.makeText(getActivity(), "Data added to API", Toast.LENGTH_SHORT).show();
try {
// on below line we are passing our response
// to json object to extract data from it.
JSONObject jsonObject = new JSONObject(response);
Log.d(TAG, "onResponse: >> SUCCESS SUCCESS");
Log.d(TAG, "onResponse: jsonObject >> " + jsonObject);
String status = (jsonObject.getString("status"));
Log.d(TAG, "onResponse: status >> " + status);
String output = (jsonObject.getString("output"));
Log.d(TAG, "onResponse: output >> " + output);
String message = (jsonObject.getString("message"));
Log.d(TAG, "onResponse: message >> " + message);
if (status.equals("1")) {
String balance = fetchBalance(email, token);
tvBalance.setText(balance);
} else {
Toast.makeText(getActivity(),
"Unable to check wallet. Please Try again", Toast.LENGTH_LONG).show();
}
// on below line we are setting this string s to our text view.
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// method to handle errors.
Toast.makeText(getActivity(), "Failed to get response = " + error, Toast.LENGTH_SHORT).show();
Log.d(TAG, "onErrorResponse: ERROR IS : >> "+ error.getMessage());
Log.d(TAG, "onErrorResponse: ERROR IS : >> "+ error.getStackTrace());
Log.d(TAG, "onErrorResponse: ERROR IS : >> "+ error.getLocalizedMessage());
Log.d(TAG, "onErrorResponse: ERROR IS : >> "+ error);
}
}) {
@Override
protected Map<String, String> getParams() {
// below line we are creating a map for
// storing our values in key and value pair.
Map<String, String> params = new HashMap<String, String>();
params.put("email", theEmail);
params.put("token", theToken);
// at last we are
// returning our params.
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json");
params.put("Accept", "application/json");
return params;
}
};
request.setRetryPolicy(new RetryPolicy() {
@Override
public int getCurrentTimeout() {
return 30000;
}
@Override
public int getCurrentRetryCount() {
return 30000;
}
@Override
public void retry(VolleyError error) throws VolleyError {
}
});
// below line is to make
// a json object request.
queue.add(request);
}
--------编辑----- 我添加了邮递员的截图
谢谢大家,问题是我的后端工程师传递的响应代码一旦更改为 201 截取开始接收响应