Search code examples
androidandroid-asynctaskget

Getting statusCode of 400 from GET


I am trying to do a get from the API, but it returns a 400, I have tried putting my local IP and putting 10.0.2.2, but he has continued to do the same, in the Event Log shows me this too: Emulator: CANNOT TRANSLATE guest DNS ip.From the PostMan I get the JSON back correctly so I do not think the problem is in the API

This is where I do everything

    public void PendingTrajects(){


     AsyncHttpClient client = new AsyncHttpClient();
       String URL = "http://ip:port/api/trajectes";
       client.get(this, URL, new AsyncHttpResponseHandler() {

       @Override
       public void onStart() {
           Toast.makeText(PendingTrajectRecyclerView.this, "Carregant...", Toast.LENGTH_SHORT).show();
       }

       @Override
       public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
           JSONArray Usuaris = new JSONArray();
           JSONArray trajectes = new JSONArray();

           String Nom = null;

           String strResponseBody = new String(responseBody);

           try {
               //Usuari = new JSONArray(strResponseBody);
               trajectes = new JSONArray(strResponseBody);
           }catch (JSONException e){
               Toast.makeText(PendingTrajectRecyclerView.this, "Error a la connexió", Toast.LENGTH_SHORT).show();
           }

           try {
               for(int i = 0; i < trajectes.length(); i++){
                   JSONObject trajecte = trajectes.getJSONObject(i);
                   JSONArray realitzas = trajecte.getJSONArray("realitzas");
                   for(int j = 0; j < realitzas.length(); j++){
                       JSONObject realitza = realitzas.getJSONObject(j);
                       JSONObject usuari = realitza.getJSONObject("usuari");
                       Log.d("xd", ""+usuari.getString("name"));
                   }
               }
           }catch (JSONException e){
               Toast.makeText(PendingTrajectRecyclerView.this, "Error a la connexió", Toast.LENGTH_SHORT).show();
           }
       }

       @Override
       public void onFailure(int statusCode, cz.msebera.android.httpclient.Header[] headers, byte[] responseBody, Throwable error) {
           Toast.makeText(PendingTrajectRecyclerView.this, "Error a la connexió ONFailure", Toast.LENGTH_SHORT).show();
           Log.d("Error",""+statusCode);
       }

   });

Solution

  • 400 BAD REQUEST

    The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

    You should DEBUG your Application. Check your parameters. Add a Break-point here

     String URL = "http://ip:port/api/trajectes";
    

    Actually server could not understand the request due to invalid syntax.