简体   繁体   English

Android设备发出HTTP请求时给出超时错误

[英]Android device giving timeout error when making http request

I am getting a timeout error on my request in my android app. 我在我的android应用中收到关于我的请求的超时错误。 I have set the retry policy but it did not solve the issue. 我已经设置了重试策略,但无法解决问题。 When tested on my emulator it works fine with no error, but when using a real device to test it gives the timeout error. 在我的仿真器上进行测试时,它可以正常工作且没有错误,但是在使用真实设备进行测试时,则会出现超时错误。

public void makeRequest(final String user, final String cred)
    {
        String url = "http://10.0.2.2:8888/map/api/login";

        StringRequest postRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>()
                {
                    @Override
                    public void onResponse(String response)
                    {
                        try
                        {
                            JSONObject jsonResponse = new JSONObject(response);
                            String status = jsonResponse.getString("status");
                            String token = jsonResponse.getString("token");


                            if(status.equalsIgnoreCase("error"))
                            {
                                Snackbar.make(findViewById(R.id.myCoordinatorLayout), jsonResponse.getString("message"), Snackbar.LENGTH_LONG).show();
                            }
                            else if (status.equalsIgnoreCase("success"))
                            {
                                System.out.println(jsonResponse);
                                Intent loader = new Intent(home.this,webViewActivity.class);
                                loader.putExtra(EXTRA_MESSAGE,token);
                                startActivity(loader);
                            }

                        }
                        catch (JSONException e)
                        {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError error)
                    {
                        error.printStackTrace();
                    }
                }
        ){
            @Override
            protected Map<String, String> getParams()
            {
                Map<String, String> params = new HashMap<>();
                params.put("portal[username]", user);
                params.put("portal[password]", cred);
                params.put("portal[From]","web");
                return params;
            }
        };
        postRequest.setRetryPolicy(new DefaultRetryPolicy(
                7000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        Volley.newRequestQueue(getApplicationContext()).add(postRequest);


    }

The error i get is below 我得到的错误如下

08-18 12:42:46.341 16112-16112/com.mobile.map.map_mobile W/System.err: com.android.volley.TimeoutError
08-18 12:42:46.341 16112-16112/com.mobile.map.map_mobile W/System.err:     at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:141)
08-18 12:42:46.341 16112-16112/com.mobile.map.map_mobile W/System.err:     at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)

Can you check on the different device and on the different network. 您可以在不同的设备和不同的网络上进行检查吗? If it doesn't work please check the response time in postman if it is more than 2500 millisecond(volley default timeout), increase the volley's default timeout in DefaultRetryPolicy.class 如果不起作用,请检查邮递员的响应时间是否超过2500毫秒(齐射默认超时),请在DefaultRetryPolicy.class中增加齐射的默认超时时间

I am assuming that your Key value pair is for Body. 我假设您的“键/值”对适用于“正文”。

        JSONObject params = new JSONObject();

            try {
                params.put("portal[username]", user);
                params.put("portal[password]", cred);
                params.put("portal[From]","web");
            } catch (JSONException e) {
               // Do something
            }

        Response.Listener<JSONObject> jsonObjectListener = new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    // Response here
                }
            };

        Response.ErrorListener errorListener = new  Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // Error here
                }
            };

       String url = "http://10.0.2.2:8888/map/api/login";

       JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, url, params,
                    jsonObjectListener, errorListener);

       Volley.newRequestQueue(getApplicationContext()).add(jsonRequest);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM