BasicNetwork.performRequest: Unexpected response code 500 for

喜你入骨 提交于 2019-12-23 02:55:21

问题


I have this Login Java code in my Android Studio:

private void loginUser(){
        pd = ProgressDialog.show(LoginActivity.this, "", "Loading...");
        StringRequest stringRequest = new StringRequest(Request.Method.POST, LOGIN_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonResponse = new JSONObject(response);
                            System.out.println("JSON RESPONSE: " + jsonResponse.toString());
                            boolean success = jsonResponse.getBoolean("success");
                            if (success) {
                                launchHomeScreen();
                                pd.dismiss();
                                Toast.makeText(LoginActivity.this,"Welcome back " + username,Toast.LENGTH_LONG).show();

                                SharedPreferences sharedPref = getSharedPreferences("loginDatas", Context.MODE_PRIVATE);

                                SharedPreferences.Editor editor = sharedPref.edit();
                                editor.putString("username", username);
                                editor.putString("password", password);
                                editor.apply();
                            }
                            else {
                                loginButton.setBackgroundColor(0x73000000);
                                Toast.makeText(LoginActivity.this,"Wrong Username or Password!",Toast.LENGTH_LONG).show();
                                pd.dismiss();
                            }
                        }
                        catch (JSONException e) {
                            loginButton.setBackgroundColor(0x73000000);
                            e.printStackTrace();
                            pd.dismiss();
                            Toast.makeText(LoginActivity.this,response,Toast.LENGTH_LONG).show();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        loginButton.setBackgroundColor(0x73000000);
                        pd.dismiss();
                        System.out.println("Error: " + error);
                    }
                }){
            @Override
            protected Map<String,String> getParams(){
                Map<String,String> params = new HashMap<>();
                params.put(KEY_USERNAME,username);
                params.put(KEY_PASSWORD,password);
                return params;
            }

        };
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }

But everytime I get this error in my Android Log console:

 I tried so many things but everytime I try it, I get back: 04-20 07:44:18.463 3326-2366/com.lolol.gg E/Volley: [294] BasicNetwork.performRequest: Unexpected response code 500 for http://lollipop.xyz/login.php
    04-20 07:44:18.514 3326-3326/com.lolol.ggI/System.out: Error: com.android.volley.ServerError

Has anybody an Idea, how to fix my problem? Because I tried many things...It won't work anyway... The weired thing is: I have tested the server code very often it works. When I try the javacode with the url for my localwampserver it works....When I try it with the url from my hostinger server it didn't work...In the 2 servers are the same codes...


回答1:


Getting an HTTP response code of 500 means that your app successfully contacted the server, but the server itself has encountered an unexpected error.

Contact your backend maintainer to see why the server is misbehaving, this most probably isn't an issue you can fix in your Android code.




回答2:


Looks like a server side error, first make sure your service work (using Postman for instance or any rest client).

If the problem persists you could try using Retrofit2 instead of Volley (which I personally prefer)




回答3:


I have used stringrequest to get response from the server and pass your params.

StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {JSONObject jsonObject = new JSONObject(response);
            } catch (JSONException ignored) {
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            if (volleyError instanceof TimeoutError) {
            }
        }
    }) {
        @Override
        public Map<String, String> getParams() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<>();
            headers.put("name", "Droider");
            return headers;
        }

        @Override
        public Priority getPriority() {
            return Priority.IMMEDIATE;
        }
    };
    ApplicationController.getInstance().addToRequestQueue(stringRequest);


来源:https://stackoverflow.com/questions/43513377/basicnetwork-performrequest-unexpected-response-code-500-for

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!