Volley returns no network response

Viewed 64

I'm trying to show a token from one website on my mobile phone. I'm using the Volley to send a GET request, but I get no response (networkResponse == null). My code is fine because I can connect f.e. to google. I found that maybe the website's response comes after the execution of a program.

How can I slow down the execution or how can I enforce waiting for the response? Or maybe can I do something else? Also, the website that I'm trying to connect works well because I can execute the request using Postman. Any ideas how to resolve it are also appreciated.

My code:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView textView = (TextView) findViewById(R.id.codeC);

        TextView textView2 = (TextView) findViewById(R.id.blad);
        RequestQueue queue = Volley.newRequestQueue(this);
        String url = "http://ec2-3-15-216-171.us-east-2.compute.amazonaws.com:3000/pairing";
        // String url ="https://google.com";
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        // Display the first 500 characters of the response string.
                        textView.setText("Response is: " + response.substring(0, 130));
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (error == null) {
                    textView2.setText("error1");
                    return;
                }
                if (error.networkResponse == null) {
                    textView2.setText("error2");
                    return;
                }
                String body;
                //get status code here
                final String statusCode = String.valueOf(error.networkResponse.statusCode);
                //get response body and parse with appropriate encoding
                try {
                    body = new String(error.networkResponse.data, "UTF-8");
                    textView2.setText(body);
                } catch (UnsupportedEncodingException e) {
                    // exception
                    textView2.setText("bbb");
                }
            }
        });
        stringRequest.setRetryPolicy(new DefaultRetryPolicy(
                500000,
                1,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        queue.add(stringRequest);
    }
}
0 Answers
Related