Bhringesh
Bhringesh

Reputation: 29

Api work with Postman but not in android application

A method build in Java using Jersey which takes two parameters and store in database it works properly with the postman but when I use it in Android Application it not work. I try to send a request using Volley and Retrofit.

Server Side Code:

@POST
@Produces(MediaType.APPLICATION_JSON)
@Path("/register")
public Boolean registerUser(@FormParam("userName") String userName, @FormParam("password") String password) {
    System.out.println(userName+"\t"+password);
    String insertQuery = "INSERT INTO user(user_name,password,status) VALUES(?,?,?)";
    try {
        Connection con = MyConnection.getConnection();
        PreparedStatement prst = con.prepareStatement(insertQuery);
        prst.setString(1, userName);
        prst.setString(2, password);
        prst.setInt(3, 0);
        int count = prst.executeUpdate();
        con.close();
        System.out.println(count+" Row inserted");
        return true;
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return false;
}

Android Code :

public void register(final String userName, final String password) {

    User user = new User(userName, password, 1);
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://192.168.1.13:8080/Demo_Application/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    JsonPlaceholderApi jsonPlaceholderApi = retrofit.create(JsonPlaceholderApi.class);

    Call<List<User>> call = jsonPlaceholderApi.register("application/x-www-form-urlencoded", user);
    call.enqueue(new Callback<List<User>>() {
        @Override
        public void onResponse(Call<List<User>> call, Response<List<User>> response) {

            if (!response.isSuccessful()){
                Log.e("Response","Something went wrong."+response.toString());
                return;
            }

            Log.d("Response",response.toString());

        }

        @Override
        public void onFailure(Call<List<User>> call, Throwable t) {
            Log.e("Response",t.getMessage());
        }
    });
}

Postman Response

Volley Request:

public void registerVolley(final String userName, final String password){

    Map<String, String> param = new HashMap<>();
    param.put("userName", userName);
    param.put("password",password);

    JsonObjectRequest arrayRequest = new JsonObjectRequest(Request.Method.POST, "http://192.168.0.26:8080/Demo_Application/rs/test/register", new JSONObject(param), new com.android.volley.Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            Log.e("Response", response.toString());
        }
    }, new com.android.volley.Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("Response", error.toString());
        }
    }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> param = new HashMap<>();
            param.put("userName", userName);
            param.put("password",password);

            return param;
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> header =  new HashMap<>();
            header.put("Content-Type","application/json");
            return header;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(arrayRequest);

}

Upvotes: 1

Views: 7937

Answers (2)

Ketan Ramani
Ketan Ramani

Reputation: 5773

Add Below Code in proguard-rules.pro

-keepattributes *Annotation*
-keepclassmembers class ** {
    @org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }
-keep class com.app.appname.model.** { *; }

NOTE: Change last line with your model folder

Upvotes: 0

Simal
Simal

Reputation: 85

Your code for retrofit:

JsonPlaceholderApi  jsonPlaceholderApi  = retrofit.create(JsonPlaceholderApi.class);
    Call<Boolean> call = jsonPlaceholderApi.sign("userName", "password");
    call.enqueue(new Callback<Boolean>() {
        @Override
        public void onResponse(Call<Boolean> call, Response<Boolean> response) {

            if (!response.isSuccessful()){
                Log.e("Response","Something went wrong."+response.toString());
                return;
            }

            Log.d("Response",response.toString());

        }

        @Override
        public void onFailure(Call<Boolean> call, Throwable t) {
            Log.e("Response",t.getMessage());
        }
    });

Your method inside jsonPlaceholderApi :

@FormUrlEncoded
    @POST("rs/test/register")
    Call<ResponseLogin> signIn(
            @Field("userName") String userName,
            @Field("password") String password
    );

Upvotes: 0

Related Questions