2

I'm trying to run a simple test app for learning Retrofit. The onFailure method is always called, please help me. All I've done is as follows:

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();
}

test_retrofit.php: //located at www directory (running wampserver on my computer)

<?php 
$response = array("error" => FALSE);
if ( isset($_POST['username']) )
{
    $response["username"] = "moker";
    $response["password"] = "0107";
    echo json_encode($response);
}
else
{
    $response["username"] = "mok";
    $response["password"] = "107";
    echo json_encode($response);
}
?>

Edit: (After iagreen's useful hint)

Exception is:

java.net.SocketTimeoutException: failed to connect to /192.168.1.7 (port 80) after 10000ms

Mohsen Kamrani
  • 7,177
  • 5
  • 42
  • 66
  • please add logs so we can know more setLogLevel(RestAdapter.LogLevel.FULL) – Laurent Russier Nov 13 '15 at 20:10
  • @LaurentRussier "Retrofit 2 doesn’t integrate any logging functionality anymore." https://futurestud.io/blog/retrofit-using-the-log-level-to-debug-requests/ :(( – Mohsen Kamrani Nov 13 '15 at 20:13
  • 1
    yeah ... this is why it's still beta, but you can try this http://stackoverflow.com/questions/32514410/logging-with-retrofit-2 to get log – Laurent Russier Nov 13 '15 at 20:14
  • @LaurentRussier I was reading it :) – Mohsen Kamrani Nov 13 '15 at 20:18
  • 1
    Add t.printStackTrace () to your onFailure () method. That will give a better idea of the cause. – iagreen Nov 13 '15 at 20:26
  • @iagreen Thanks it worked. I'll add the stackTrace now. – Mohsen Kamrani Nov 13 '15 at 20:30
  • Looks like your web server is either not running, you have the IP wrong, or it is not running on port 80. – iagreen Nov 13 '15 at 20:42
  • @iagreen No. I just tested http://192.168.1.7/test_retrofit.php and it returns {"error":false,"username":"mok","password":"107"} – Mohsen Kamrani Nov 13 '15 at 20:43
  • 1
    Did you test it from the same device your are testing retrofit on? The other option is the device is on a different network. 192.168.x.x is a private IP, so if the phone is not on the same subnet it won't find the server. – iagreen Nov 13 '15 at 20:44
  • @iagreen Thank you. I fully forgot to enable the firewall rule that allows connecting to the port 80. please post your comment as the answer and I'll accept it. – Mohsen Kamrani Nov 13 '15 at 20:51

2 Answers2

1

It looks like your device cannot connect to the server. Did you test it from the same device your are testing retrofit on? The other option is the device is on a different network. 192.168.x.x is a private IP, so if the phone is not on the same subnet it won't find the server. Or, as it turned out, you need to make sure your firewall is allowing connections to your server.

iagreen
  • 31,470
  • 8
  • 76
  • 90
0

Remove the last slash of your BASE_URL and put it at the start of @POST("test_retrofit.php") => @POST("/test_retrofit.php")

Laurent Russier
  • 658
  • 5
  • 24