Reputation: 1212
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.
Upvotes: 0
Views: 285
Reputation: 14193
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.
Upvotes: 1
Reputation: 527
in onFailure method, capture Throweble with Log.e("failure", t.toString());
Then you can find the exact problem with the respond.
Upvotes: 2