How to read fixes within JSON objects with retrofit 2 in android? error: "java.net.ConnectException: Failed to connect to"

3

I am trying to consume a api of a localhost , once created generate the following JSON

{"Users":[
         {"id":1,
          "username":"pepe123",
          "fname":"pepe",
          "lname":"jose",
          "dispositivo":[
                      {"clave":"1234",
                       "codigo":"678"},                                     
                      {"clave":"3423",
                       "codigo":"444"}]}]}

Which will be consumed by my App of android , I'm using retrofit and by means of the page http://www.jsonschema2pojo.org/ I get the respective clases to work the data of JSON , I'm doing a login and I want to access the data "clave" and "codigo" that are my validators to allow me to enter the App , here my classes and my adapter retrofit

public interface API {

public static final String BASE_URL = "http://127.0.0.1:8000/";

@GET("Apidata")
Call<Responsejson>  getdata();

}

The clases generated by http://www.jsonschema2pojo.org/ :

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class dispositivo {

@SerializedName("clave")
@Expose
private String clave;
@SerializedName("codigo")
@Expose
private String codigo;

public String getClave() {
return clave;
}

public void setClave(String clave) {
this.clave = clave;
}

public String getCodigo() {
return codigo;
}

public void setCodigo(String codigo) {
this.codigo = codigo;
}

}

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Responsejson {

@SerializedName("Users")
@Expose
private List<User> users = null;

public List<User> getUsers() {
return users;
}

public void setUsers(List<User> users) {
this.users = users;
}

}

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class User {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("username")
@Expose
private String username;
@SerializedName("fname")
@Expose
private String fname;
@SerializedName("lname")
@Expose
private String lname;
@SerializedName("dispositivo")
@Expose
private List<dispositivo> Dispositivo = null;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getFname() {
return fname;
}

public void setFname(String fname) {
this.fname = fname;
}

public String getLname() {
return lname;
}

public void setLname(String lname) {
this.lname = lname;
}

public List<dispositivo> getDispositivo() {
return Dispositivo;
}

public void setDispositivo(List<dispositivo> Dispositvo) {
this.Dispositivo = Dispositivo;
}

}

And here I make the call:

Call<Responsejson> users = API.getdata();
        users.enqueue(new Callback<Responsejson>() {
            @Override
            public void onResponse(Call<Responsejson> call, Response<Responsejson> response) {
                if(response.isSuccessful()){
                    Responsejson responsejson = response.body();
                    Toast.makeText(LoginActivity.this, "Obtenido"+responsejson, Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<Responsejson> call, Throwable t) {

            }
        });

But when I run the application it does not send me any data, or any data attempt, I do not know if I'm doing something wrong, or I'm missing something, but in log the only thing it says is: p>

D/OkHttp: --> GET http://127.0.0.1:8000/Apidata
    --> END GET
D/OkHttp: <-- HTTP FAILED: java.net.ConnectException: Failed to connect to /127.0.0.1:8000

Could you give me some idea why this happens? thanks in advance.

    
asked by David Villegas 31.10.2018 в 20:53
source

1 answer

0

The problem is not due to the code, you are not really accessing

 http://127.0.0.1:8000/Apidata

First you must remember that you need to define within your file AndroidManifest.xml permission for internet use:

<uses-permission android:name="android.permission.INTERNET" />

The specific error is that the url you are trying to access by making a request GET is not actually available:

http://127.0.0.1:8000/Apidata

This may be due to some causes such as:

  • The LocalHost is not activated
  • Check if port 8000 is correct.
  • Your firewall may not be allowing access.
answered by 31.10.2018 / 21:16
source