调用Retrofit-Callback.onFailure

时间:2015-11-13 19:52:28

标签: php android retrofit

我正在尝试运行一个简单的测试应用程序来学习Retrofit。总是调用CREATE TABLE new_table_name AS SELECT * FROM old_table_name WHERE 1 = 2; 方法,请帮助我。我所做的一切如下:

onFailure

MainActivity

public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); method(); } public void method() { final String BASE_URL = "http://192.168.1.7/"; Retrofit retrofit = new Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .build(); ApiService service = retrofit.create(ApiService.class); Call<ResponseBody> call = service.login(); call.enqueue(new Callback<ResponseBody>(){ @Override public void onResponse(Response<ResponseBody> response, Retrofit retrofit) { // TODO Auto-generated method stub if (response.isSuccess()) { Log.i("mok","S"); ResponseBody rb = response.body(); } else { Log.i("mok","F"); com.squareup.okhttp.ResponseBody rb = response.errorBody(); } } @Override public void onFailure(Throwable t) { Log.i("mok",t.getCause()+"");//This is null Log.i("mok","T");//This is shown in LogCat finish(); } }); } }

RespnseBody

public class ResponseBody { private String username; private String password; public ResponseBody(String username, String password) { this.setUsername(username); this.setPassword(password); } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }

ApiService

public interface ApiService { @POST("test_retrofit.php") public Call<ResponseBody> login(); } //位于www目录(在我的计算机上运行wampserver)

test_retrofit.php

编辑:(在iagreen的有用提示之后)

例外是:

java.net.SocketTimeoutException:10000ms后无法连接到/192.168.1.7(端口80)

2 个答案:

答案 0 :(得分:1)

您的设备似乎无法连接到服务器。您是否正在测试改装的同一设备上进行测试?另一种选择是设备位于不同的网络上。 192.168.x.x是私有IP,因此如果手机不在同一子网上,则无法找到服务器。或者,事实证明,您需要确保您的防火墙允许连接到您的服务器。

答案 1 :(得分:0)

删除BASE_URL的最后一个斜线并将其放在@POST的开头(&#34; test_retrofit.php&#34;)=&gt; @POST(&#34; /test_retrofit.php")