Unexpected response code.

I am having the "Unexpected 500 response code" problem when I get the API URL.

This is my volleyball library code:

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); } 

And I set the Internet permission to my manifest.

If the URL is copied in the browser, this is the answer, I can access it, but if I put in my code, I will get Error 500.

NB : The URL of this is only IP addresses, because it is the IP address of my VPS server, and I have not configured my server domain yet ...

How can i fix this? Sorry for my English.

+6
source share
2 answers

This is not a problem with your library, browsers usually ignore the 500 error in the headers and display the content, but the volley dispenser does not do this and raises onErrorResponse. I think this is a server side problem, but you can get the answer in onErrorResponse. in order to convince yourself, you can add RESTClient mozilla and send your request for a volley and see the answer. in the header, it shows the error of the internal server 500 error, but you can see the answer in the body section

+5
source

Here is a solution to this error. First, you need to extend the Reponse class and change @Override protected Response parseNetworkResponse(NetworkResponse response) { for example:

 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( 

and when listening for errors:

 @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(); } 
+1
source

Source: https://habr.com/ru/post/970608/


All Articles