Apo*_*olo 5 android timeoutexception retrofit2
我正在尝试使用 Retrofit 2 使我的应用程序连接到本地 Web 服务,但我总是收到此错误。我确定 Web 服务正在响应,因为我在 firefox 中使用了一个工具来发出 @GET 请求并且返回正常,返回正确的 JSON。
在android中它甚至没有连接。
这是我的主要活动:
public class MainActivity extends AppCompatActivity {
private String API_BASE_URL="http://192.168.1.32:8080/economarket-restService/resources/androidTest/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.client(
httpClient.build()
).build();
ContatoService contato = retrofit.create(ContatoService.class);
Call<Contato> repos = contato.listRespos(); //EconomarketService
repos.enqueue(new Callback<Contato>() {
@Override
public void onResponse(Call<Contato> call, Response<Contato> response) {
Contato contato = response.body();
Toast.makeText(getBaseContext(), "Return" + contato.getName(), Toast.LENGTH_LONG).show();
Log.d("Retorno",response.toString());
}
@Override
public void onFailure(Call<Contato> call, Throwable t) {
Toast.makeText(getBaseContext(), "Return" + t.toString(), Toast.LENGTH_LONG).show();
Log.d("Retorno",t.toString());
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
我的界面:
public interface ContatoService {
@GET("retorna/")
Call<Contato> listRespos();
}
Run Code Online (Sandbox Code Playgroud)
模型类(Contato):
public class Contato {
@SerializedName("name")
private String name;
@SerializedName("phone")
private int phone;
@SerializedName("likes")
private int likes;
@SerializedName("location")
private Location location;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPhone() {
return phone;
}
public void setPhone(int phone) {
this.phone = phone;
}
public int getLikes() {
return likes;
}
public void setLikes(int likes) {
this.likes = likes;
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
}
Run Code Online (Sandbox Code Playgroud)
模型类(位置):
public class Location{
}
Run Code Online (Sandbox Code Playgroud)
问题解决了!问题是API_BASE_URL,网址应该只有:
http://192.168.1.32:8080/
其余的 url 应该在界面上:
\n@GET("economarket-restService/resources/androidTest/retorna/\xe2\x80\x8c\xe2\x80\x8b").\nRun Code Online (Sandbox Code Playgroud)\n这一切都归结为在 中声明 URL 的根,URL_BASE并且 url 目录访问必须在界面上。
| 归档时间: |
|
| 查看次数: |
10248 次 |
| 最近记录: |