I've a server running in my localhost with some information in the database that i want to show in my Android application, I'm using the retrofit library to do it but I only get this message:
error loading from API
Here's my RetrofitClient:
public class RetrofitClient {
private static Retrofit retrofit = null;
public static Retrofit getClient(String baseUrl) {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
Here is the class with my GET method:
public interface SOService {
@GET("stocks/")
Call<List<Stock>> getStocks();
}
Here's my class with the url and how to connect:
public class ApiUtils {
public static final String BASE_URL = "http://127.0.0.1:8000";
public static SOService getSOService() {
return RetrofitClient.getClient(BASE_URL).create(SOService.class);
}
}
And here's where I finally try to obtain the information:
public class RegisterActivity extends AppCompatActivity {
private Button registerButton;
private SOService mService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mService = ApiUtils.getSOService();
registerButton = findViewById(R.id.register_button);
registerButton.setOnClickListener(v -> loadAnswers());
}
public void loadAnswers() {
System.out.println("Entro aquí");
mService.getStocks().enqueue(new Callback<List<Stock>>() {
@Override
public void onResponse(Call<List<Stock>> call, Response<List<Stock>> response) {
if(response.isSuccessful()) {
System.out.println("Te lo imprimo");
System.out.println(response.body().toString());
Log.d("MainActivity", "posts loaded from API");
}else {
int statusCode = response.code();
// handle request errors depending on status code
}
}
@Override
public void onFailure(Call<List<Stock>> call, Throwable t) {
Log.d("MainActivity", "error loading from API");
}
});
}
}
I've the class Stock like this:
public class Stock {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("ticker")
@Expose
private String ticker;
@SerializedName("open")
@Expose
private Double open;
@SerializedName("close")
@Expose
private Double close;
@SerializedName("volume")
@Expose
private Integer volume;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTicker() {
return ticker;
}
public void setTicker(String ticker) {
this.ticker = ticker;
}
public Double getOpen() {
return open;
}
public void setOpen(Double open) {
this.open = open;
}
public Double getClose() {
return close;
}
public void setClose(Double close) {
this.close = close;
}
public Integer getVolume() {
return volume;
}
public void setVolume(Integer volume) {
this.volume = volume;
}
}
Any idea why it dosn't work?
Here's the error that I get:
W/System.err: java.net.ConnectException: Failed to connect to /127.0.0.1:8000 at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:242) W/System.err: at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:160) at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:257) at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135) at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114) at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42) W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) W/System.err: at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) W/System.err: at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200) at okhttp3.RealCall$AsyncCall.execute(RealCall.java:147) at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) at java.lang.Thread.run(Thread.java:764) W/System.err: Caused by: java.net.ConnectException: failed to connect to /127.0.0.1 (port 8000) from /127.0.0.1 (port 42532) after 10000ms: isConnected failed: ECONNREFUSED (Connection refused) at libcore.io.IoBridge.isConnected(IoBridge.java:273) at libcore.io.IoBridge.connectErrno(IoBridge.java:188) at libcore.io.IoBridge.connect(IoBridge.java:130) at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:129) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:356) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356) W/System.err: at java.net.Socket.connect(Socket.java:616) at okhttp3.internal.platform.AndroidPlatform.connectSocket(AndroidPlatform.java:71) at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:240) ... 21 more Caused by: android.system.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused) W/System.err: at libcore.io.IoBridge.isConnected(IoBridge.java:262) ... 31 more