Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple retrofit2 request to a localhost server

trying to make an android app that will communicate with a localhost server (tomcat apache) which use noSQL server (app->tomcat->noSQL). I already manage to make a servlet that handle params on "get" method and load them correctly to the database, now I am trying to insert the data from my app using retrofit2 lib. following vids and tutorials I still couldnt manage to make this work.

this is the interface I am using:

public interface APIService {

    @POST("login")
    Call<Boolean> postUser(@Body User user);

    @GET("login")
    Call<Boolean> getUser(@Query("user_email") String user_email,@Query("user_pass") String user_pass);

    public static final Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://localhost:8080/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
}

and this is the code I am using when the button is clicked in the app:

APIService ApiService = APIService.retrofit.create(APIService.class);

User user = new User(name, email);
Call<Boolean> call = ApiService.getUser(email,name);
call.enqueue(new Callback<Boolean>() {
    @Override
    public void onResponse(Call<Boolean> call, Response<Boolean> response) {
        String ans = response.message(); //for debugging
        if (ans.compareTo("yes") == 0) {
            Toast.makeText(getApplicationContext(), "YES!", Toast.LENGTH_SHORT).show();
        } else if (ans.compareTo("no") == 0) {
            Toast.makeText(getApplicationContext(), "NO!", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getApplicationContext(), "ELSE?!", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFailure(Call<Boolean> call, Throwable t) {
        Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
    }



});

so atm, nothing happen when I am clicking the button.(it used to crush but it stopped) and I am sure the the button's function is being called.

like image 951
Etian Chamay Avatar asked Oct 17 '16 03:10

Etian Chamay


People also ask

What is retrofit2?

What is Retrofit. Retrofit is a REST Client for Java and Android allowing to retrieve and upload JSON (or other structured data) via a REST based You can configure which converters are used for the data serialization, example GSON for JSON.

How do I retrofit an API call?

If we want to consume the API asynchronously, we call the service as follows: String username = "sarahjean"; Call<User> call = apiService. getUser(username); call. enqueue(new Callback<User>() { @Override public void onResponse(Call<User> call, Response<User> response) { int statusCode = response.

Is retrofit REST API?

Retrofit is type-safe REST client for Android and Java which aims to make it easier to consume RESTful web services.


1 Answers

If you are using emulator, then change URL to http://10.0.2.2:8080/.

like image 189
Altaf Shaikh Avatar answered Oct 13 '22 00:10

Altaf Shaikh