BasicNetwork.performRequest: Unexpected response code 500 for http://192.168.1.195:85/tranxavApi/public/api/signup


Problem:

Maybe this problem has been posted before but it does not solve my case. I am trying to send data from android to mysql database using laravel as the backend. I have developed the restful Api. This is my code.

         try {
                RequestQueue requestQueue = Volley.newRequestQueue(SignUpActivity.this);

                JSONObject jsonBody = new JSONObject();
                jsonBody.put("username","amz");
                jsonBody.put("email","[email protected]");
                jsonBody.put("password","fashion");
                jsonBody.put("vCode","12345");
                jsonBody.put("type","coupons");
                final String requestBody = jsonBody.toString();

                StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.i("VOLLEY", response);
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("VOLLEY", error.toString());
                    }
                }) {
                    @Override
                    public String getBodyContentType() {
                        return "application/json; charset=utf-8";
                    }

                    @Override
                    public byte[] getBody() throws AuthFailureError {
                        try {
                            return requestBody == null ? null : requestBody.getBytes("utf-8");
                        } catch (UnsupportedEncodingException uee) {
                            VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
                            return null;
                        } catch (Exception e) {
                            e.printStackTrace();
                            return null;
                        }
                    }
                    @Override
                    protected Response<String> parseNetworkResponse(NetworkResponse response) {
                        String responseString = "";
                        if (response != null) {
                            responseString = String.valueOf(response.statusCode);
                            // can get more details such as response.headers
                        }
                        return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
                    }
                };
                requestQueue.add(stringRequest);
            } catch (JSONException e) {
                e.printStackTrace();
            }

Api URL

http://192.168.1.195:85/tranxavApi/public/api/signup

E/VOLLEY: com.android.volley.NetworkError

Laravel Code

 public function signUp(Request $request){
    $success = "Successful";
    $validator = Validator::make($request->all(), [
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|min:5'
    ]);

    if ($validator->fails()){
        return response()->json(['error'=>$validator->errors()], 401);
    }
    User::create([
        'email' => $request->get('email'),
        'username' => $request->get('username'),
        'password' => Hash::make($request->get('password')),
        'vCode' => $request->get('vCode')
    ]);

    return response()->json(['success'=> $success], $this->successStatus);
}

My error Log

E/Volley: [164] BasicNetwork.performRequest: Unexpected response code 500 for http://192.168.1.195:85/tranxavApi/public/api/signup

E/VOLLEY: com.android.volley.NetworkError

No authorization is needed. All your answers will be highly appreciated


Solution:

Add this to your code

{     
        @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;  
        }
    };

Recent Tips

  1. Laravel 5.1 Authentication without password
  2. django-bootstrap-v5 DecimalField Throwing Errors in ModelForm Class
  3. update and delete in realm xamarin
  4. Grabbing text between Processing Instructions in XSLT
  5. Foreign key refering to multiple tables
  6. Run a visual studio 2010 Windows7 project on a Windows XP machine
  7. How to apply groupname to HTML radio buttons in asp.net?
  8. What is the most stable modal dialog implementation across browsers to use from javascript for a web app?
  9. Move, Remove, and Replace information from Google Sheets with new entries using Google Script
  10. How can I get rid of jerkiness in WinForms scrolling animation?
  11. Update MySQL table column from another table entities
  12. Shoot fireball once in unity C#
  13. Adding a table of contents to a Microsoft Word Document using vbs
  14. Change locale in android app (onto Hindi)
  15. How do I stop selenium automation if 20% or 1st 20 test cases test methods are failed?
  16. Sharepoint with silverlight app
  17. What's the best way to write robots.txt for github pages using multiple repos?
  18. Biopython: Cant use .count() for biopython
  19. How can I find out the token balance of an address?
  20. ref value is undefined in vue (modal, textarea, $refs)
  21. Azure - HDInsight Hbase Data Insertion Failed
  22. SignalR overwriting OnConnected(), OnDisconnected()
  23. DatePickerDialog displays with two borders
  24. "type 'double' is not a subtype of type 'int' in type cast" error in flutter. What should i do?
  25. hiding the autocomplete list when user click outside the textbox is not working as expected
  26. JSF IceFaces basic problem with redisplaying input value
  27. How to validate material ui TextField in reactjs?
  28. Go and MongoDB connection won't work with panic log "no reachable server"
  29. WordPress Posts Pagination Not Working
  30. F# sprintf won't print in interactive console