Unexpected response code 500 URL API Android app

14,338

Solution 1

This is not your library problem, browsers usually ignore error 500 in headers and display the content but volley dose not do that and call onErrorResponse. I think this is a server side application problem but you can get your response in onErrorResponse. in order to convince yourself you can add RESTClient mozilla add on and send your volley request and see the response. in the header it shows you error 500 internal server error but you can see the response in the body section

Solution 2

Here is the solution for this error. Firstly you need to extend the Reponse class and modify the @Override protected Response parseNetworkResponse(NetworkResponse response) { like this :

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 in error listener :

@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();
        }           
Share:
14,338
bukanamay
Author by

bukanamay

Updated on June 18, 2022

Comments

  • bukanamay
    bukanamay almost 2 years

    I have get problem "Unexpected response code 500", when I access URL API.

    This my code with volley library:

    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 have set permission Internet on my manifest.

    If URL I copy on browser, this is responded, I can accesses but if I put in my code, I get Error 500.

    NB : URL this only IP address, cause this is IP address my VPS server and I'm not yet set domain my server...

    How can I fix it? Sorry for my english.

  • karthik prasad
    karthik prasad almost 8 years
    I can see that it works in browser. But how to handle in Volley?
  • CrazyMind
    CrazyMind over 7 years
    Facing same problem not getting solution. same service well works on ios