我可以通过retrofit2和okhttp3库与Server通信,我得到了以下
D/LT: Response successful.
Code: 200
Server: Apache-Coyote/1.1
Content-Type: text/plain;charset=UTF-8
Content-Length: 441
Date: Tue, 14 Jun 2016 08:01:23 GMT
D/LT: responseBody = null
正如你所看到的,我得到了一个成功的回复,它长达441个Chars。
那么,为什么我会得到一个空体?
这些是我的来源
MyService client = MyServiceGenerator.createService(MyService.class);
String contentType = "application/json";
String bodyContent = "{'userId': 'user', 'password': 'pwd', 'deviceId': '123', 'applicationId': '123'}";
RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"), bodyContent);
Call call = client.session(contentType, requestBody);
// Asynchronic way
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
Log.d("LT","Reached!");
if ( response.isSuccessful() ) {
Log.d("LT", "Response successful. Code: "+ response.code() + " " + response.headers());
Void decodedResponseBody = (Void)response.body();
if ( decodedResponseBody != null )
Log.d("LT", "decodedResponseBody :"+decodedResponseBody);
else
Log.d("LT", "responseBody = null");
} else {
try {
Log.d("LT", "response not successful: error: (" + response.errorBody().string()+") message: ("+response.message()+")");
if ( response != null ) {
Log.d("LT", "response != null ");
String decodedResponseBody = response.body().toString();
Log.d("LT", "Response Body: "+ decodedResponseBody+" raw: "+response.raw() + " headers: " + response.headers());
} else {
Log.d("LT", "response == null");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call call, Throwable t) {
//Handle failure
Log.d("LT", t.getMessage());
}
});
我的服务界面
public interface MyService {
@POST("sessions")
Call<Void> session(@Header("Content-Type") String contentType, @Body RequestBody body);
}
MyServiceGenerator
public class MyServiceGenerator {
public static final String API_BASE_URL = "http://...";
// Here we add an interceptor in order to be able to listen the most typical messages
private static LoggingInterceptor myLoggingInterceptor = new LoggingInterceptor();
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();//.addInterceptor(myLoggingInterceptor);
private static Retrofit.Builder builder = new Retrofit.Builder().baseUrl(API_BASE_URL).addConverterFactory(GsonConverterFactory.create());
public static <S> S createService(Class<S> serviceClass) {
Retrofit retrofit = builder.client(httpClient.build()).build();
return retrofit.create(serviceClass);
}
}
答案 0 :(得分:1)
我明白了!
我只需要序列化为ResponseBody对象。
D/LT: decodedResponseBody :okhttp3.ResponseBody$1@517fd33 decodedResponseBody.content-type: text/plain;charset=UTF-8 string: {"userId":"user","deviceId":"123","applicationId":"123","sessionToken":"DE57FDACCFD199EE9B96ADD4D6B1E30DC8B231D0DDC3B84EEEE04C68F9993210","companies":[{"name":"Bauernhof","id":"4C10E349D79149BACCFEE7C35E71EDAF11A9895215BA03BAED4746EAC7CB1E29"},{"name":"Farm","id":"280EBAB2615378E1433F7C57140E1643575C32F99C173779759947B6E8732BB6"},{"name":"Schweinfurt","id":"C5CD9569869CC93B7D89BBE7AABEF9D158AB1D9BEB79D3C317D8FA492C903EAB"}]}
这些是我的已修改来源
MyService client = MyServiceGenerator.createService(MyService.class);
String contentType = "application/json";
String bodyContent = "{'userId': 'user', 'password': 'pwd', 'deviceId': '123', 'applicationId': '123'}";
RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"), bodyContent);
Call<ResponseBody> call = client.session(contentType, requestBody);
// Asynchronic way
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Log.d("LT","Reached!");
if ( response.isSuccessful() ) {
Log.d("LT", "Response successful. Code: "+ response.code() + " " + response.headers());
ResponseBody decodedResponseBody = response.body();
if ( decodedResponseBody != null )
try {
Log.d("LT", "decodedResponseBody :"+decodedResponseBody+" decodedResponseBody.content-type: "+decodedResponseBody.contentType()+" string: "+decodedResponseBody.string());
} catch (IOException e) {
e.printStackTrace();
}
else
Log.d("LT", "responseBody == null");
} else {
try {
Log.d("LT", "response not successful: error: (" + response.errorBody().string()+") message: ("+response.message()+")");
if ( response != null ) {
Log.d("LT", "response != null ");
ResponseBody decodedResponseBody = response.body().toString();
Log.d("LT", "Response Body: "+ decodedResponseBody+" raw: "+response.raw() + " headers: " + response.headers());
} else {
Log.d("LT", "response == null");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
//Handle failure
Log.d("LT", t.getMessage());
}
});
我的服务界面
public interface MyService {
@POST("sessions")
Call<ResponseBody> session(@Header("Content-Type") String contentType, @Body RequestBody body);
}