当我访问URL API时,我遇到了问题"意外的响应代码500"
这是我的代码与volley库:
String url= "http://103.241.24.35/android/android_login_api/index.php";
public void detailURL(String url) {
Log.v("Android Spinner JSON Data Activity", url);
queue = Volley.newRequestQueue(this);
StringRequest sr = new StringRequest(Request.Method.POST,url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
ProgressLoadStartLogin.setVisibility(View.GONE);
displaystatis_kontenDetail(response);
btn_enable();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if( error instanceof NetworkError) {
} else if( error instanceof ServerError) {
} else if( error instanceof AuthFailureError) {
} else if( error instanceof ParseError) {
} else if( error instanceof NoConnectionError) {
} else if( error instanceof TimeoutError) {
}
statusKoneksi();
btn_enable();
ProgressLoadStartLogin.setVisibility(View.GONE);
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("tag",Variabel.login_tag);
params.put("username",user);
params.put("password", password);
params.put("idgcm",GCMid);
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("Content-Type","application/x-www-form-urlencoded");
return params;
}
};
queue.add(sr);
}
我已在我的清单上设置了Internet权限。
如果我在浏览器上复制了URL,则会响应,我可以访问但如果我输入了代码,则会收到错误500.
NB :这是唯一的IP地址,因为这是我的VPS服务器的IP地址,而我还没有设置我的服务器域...
我该如何解决?抱歉我的英文。
答案 0 :(得分:5)
这不是你的库问题,浏览器通常忽略标题中的错误500并显示内容,但是凌空剂量不这样做并调用onErrorResponse。我认为这是服务器端应用程序问题,但您可以在onErrorResponse中获取响应。为了说服自己,你可以添加RESTClient mozilla add并发送你的截击请求并查看响应。在标题中,它显示错误500内部服务器错误,但您可以在正文部分中看到响应
答案 1 :(得分:1)
以下是此错误的解决方案。首先,您需要扩展Reponse
类并修改@Override protected Response parseNetworkResponse(NetworkResponse response) {
像这样:
public class CustomRequest extends Request {
public CustomRequest(int method, String url, ErrorListener listener) {
super(method, url, listener);
// TODO Auto-generated constructor stub
}
@Override
protected Response parseNetworkResponse(NetworkResponse response) {
System.out.println(response.statusCode);
try {
Log.d("custom response", "[raw json]: " + (new String(response.data)));
Gson gson = new Gson();
String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
return Response.success(response.data,
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JsonSyntaxException e) {
return Response.error(new ParseError(e));
}
}
@Override
protected void deliverResponse(Object arg0) {
// TODO Auto-generated method stub
}
}
//--------------------------------------
//to make request to server we need to use our custom Request like this :
JsonObjectRequest mJsonObjectRequest = new JsonObjectRequest(
CustomRequest.Method.POST, url, obj, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
;
try {
Toast.makeText(
并在错误监听器中:
@Override
public void onErrorResponse(VolleyError arg0) {
Log.e("erro;r", "error");
if(arg0.networkResponse.statusCode==500){
Log.e("error", "" + new String(arg0.networkResponse.data));
try {
JSONObject obj = new JSONObject(new String(arg0.networkResponse.data));
int codeErreur = obj.getInt("code_erreur");
if (codeErreur == 501) {
Drawable warning = (Drawable) MonEntretienApplication.getInstance().getResources().getDrawable(R.drawable.warning_red);
warning.setBounds(new Rect(0, 0, warning.getIntrinsicWidth(), warning.getIntrinsicHeight()));
email.setError("",warning);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}