我正在尝试将哈希图发送到服务器。我可以从Postman中放入哈希图的内容,但无法从我的android项目中执行相同操作。回调首先执行Onfailure
。第二次尝试发送相同的数据时,我收到409冲突响应。
失败的堆栈跟踪:
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)
界面:
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);
}
发布功能:
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();
}
});
}
邮递员给出的请求正文:
{
"clientid":"##########",
"email":"IAzMzkaksB8KGT2SLJsol7Xr9Phts/G8",
"first_name":"Eswar",
"last_name":"",
"password":"Dt9keQfZMUY=",
"security_answer":"",
"security_question":"",
"account_type":"learning_pod_app"
}
我从这些链接中找到了一些解决方案:one,two,three ... 但是它们不起作用。
感谢您的帮助。
答案 0 :(得分:2)
在onFailure方法中,使用Log.e(“ failure”,t.toString());捕获Throweble。
然后,您可以找到响应的确切问题。
答案 1 :(得分:1)
将方法签名更改为:
@Headers("Content-Type: application/json")
@PUT("authenticate/create")
Call<HashMap<String,String>> signUpParent(@Body HashMap<String, String> hashMap);
如果响应为空,则可以使用。
@Headers("Content-Type: application/json")
@PUT("authenticate/create")
Call<ResponseBody> signUpParent(@Body HashMap<String, String> hashMap);
尝试一下。