I'm sending an http request from Android Java using the Volley library to a Spring backend. The backend application should responds with a JSON string. The Tomcat server is on 192.168.1.8:8080 (or localhost on the same machine) and from browser and with the http apache library requests works fine. On android I get some strange error:
public class MainActivity extends AppCompatActivity {
private TextView textResponse;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textResponse = findViewById(R.id.responseText);
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://192.168.1.8:8080/admin/getAll";
System.out.println(url);
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
response -> {
// Display the first 500 characters of the response string.
System.out.println("Response is: " + response.substring(0,500));
textResponse.setText(response.substring(0,50));
},
error -> {
System.out.println("That didn't work!" );
textResponse.setText("error");
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
//queue.start();
System.out.println("sto qua");
}}
It goes in the error case, and if I try to get the error status code with error.networkResponse.statusCode
it gives me:
Attempt to read from field 'int com.android.volley.NetworkResponse.statusCode' on a null
object reference
Where is the problem?