0

I am trying to send a hashmap to the server. I am able to PUT the contents of the hashmap from Postman but I am unable to do the same from my android project. The callback first executes Onfailure. On second attempt to send the same data, I get a 409 conflict response.

The stacktrace of the failure:

08-21 14:42:19.748 9876-9876/com.example.vishwa.postingdata W/System.err: java.io.EOFException: End of input at line 1 column 1 path $ 08-21 14:42:19.750 9876-9876/com.example.vishwa.postingdata W/System.err: at com.google.gson.stream.JsonReader.nextNonWhitespace(JsonReader.java:1401) at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:549) at com.google.gson.stream.JsonReader.peek(JsonReader.java:425) at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:161) at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:145) 08-21 14:42:19.751 9876-9876/com.example.vishwa.postingdata W/System.err: at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37) at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25) at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:119) at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:218) 08-21 14:42:19.752 9876-9876/com.example.vishwa.postingdata W/System.err: at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:112) at okhttp3.RealCall$AsyncCall.execute(RealCall.java:141) at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) at java.lang.Thread.run(Thread.java:818) 

The interface:

public interface APIService { @POST("/posts") @FormUrlEncoded rx.Observable<Post> savePost(@Field("id") long id, @Field("title") String title, @Field("author") String author); @PUT("/posts/{id}") @FormUrlEncoded Call<Post> updatePost(@Path("id") long id, @Field("title") String title, @Field("author") String author); @DELETE("/posts/{id}") Call<Post> deletePost(@Path("id") long id); @PUT("authenticate/create") Call<HashMap<String,String>> signUpParent(@Body HashMap<String,String> hashMap); } 

The function to post:

private void PostData(HashMap<String, String> hashMap) { Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://coreapi.xxxxx.org:8080/v1/") .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build(); APIService apiService =retrofit.create(APIService.class); apiService.signUpParent(hashMap).enqueue(new retrofit2.Callback<HashMap<String, String>>() { @Override public void onResponse(retrofit2.Call<HashMap<String, String>> call, Response<HashMap<String, String>> response) { Integer code=response.code(); Toast.makeText(getBaseContext(),"checking the response : "+code,Toast.LENGTH_LONG).show(); Log.i("success", "post submitted to API imaginators: "); } @Override public void onFailure(retrofit2.Call<HashMap<String, String>> call, Throwable t) { Log.e("failure", "Unable to submit post to API imaginators."); t.printStackTrace(); } }); } 

The request body given in the postman:

{ "clientid":"##########", "email":"IAzMzkaksB8KGT2SLJsol7Xr9Phts/G8", "first_name":"Eswar", "last_name":"", "password":"Dt9keQfZMUY=", "security_answer":"", "security_question":"", "account_type":"learning_pod_app" } 

I have found some solutions from these links: one, two, three... But they don't work.

Any help is appreciated.

4
  • I think you are sending JSON data, So you need to accept and return JSON from the API as well Commented Aug 21, 2018 at 9:26
  • Use logcat to show JSON response from the server. Commented Aug 21, 2018 at 9:27
  • @sontruongit The code directly executes onfailure, so I am not able to see response in the first attempt itself. In the second it gives, 409 response and the response.body() gives null. Commented Aug 21, 2018 at 9:34
  • 1
    put all your params of requestbody in a pojo class and then send to the server in place of hashmap Commented Aug 21, 2018 at 9:54

2 Answers 2

2

in onFailure method, capture Throweble with Log.e("failure", t.toString());

Then you can find the exact problem with the respond.

Sign up to request clarification or add additional context in comments.

Comments

1

Change your method signature to:

@Headers("Content-Type: application/json") @PUT("authenticate/create") Call<HashMap<String,String>> signUpParent(@Body HashMap<String, String> hashMap); 

If the response is empty you can use.

@Headers("Content-Type: application/json") @PUT("authenticate/create") Call<ResponseBody> signUpParent(@Body HashMap<String, String> hashMap); 

And give it a try.

5 Comments

I get this error after trying this approach: "@FieldMap parameters can only be used with form encoding. (parameter #1)". Even with the encoding, I get a 400 response.
Can you capture a screenshot of the request in Postman with body tab highlighted?
In Postman, I get a 200 response code but the body doesn't show anything.
I mean request body not response, before you hit Send button.
Thank you the second one worked, using the responsebody.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.