Android 如何在改造中解析Restfull api JSON响应?
我有一个典型的改装API请求。我使用了一个vb.net API调用,它返回的响应如下Android 如何在改造中解析Restfull api JSON响应?,android,api,retrofit,rx-java,retrofit2,Android,Api,Retrofit,Rx Java,Retrofit2,我有一个典型的改装API请求。我使用了一个vb.net API调用,它返回的响应如下 <string xmlns="http://tempuri.org/"> {"UserTickets":[{"Type":"Application","Category":"Mobile App","Sub Category":"Other","Issue Details":"Dummy","LMDT":"2019-11-14 15:46:00.000","ResponsibleEmail":"al
<string xmlns="http://tempuri.org/"> {"UserTickets":[{"Type":"Application","Category":"Mobile App","Sub Category":"Other","Issue Details":"Dummy","LMDT":"2019-11-14 15:46:00.000","ResponsibleEmail":"alex@goba.com"}],"success":"true","status:":"1"}</string>
{“用户票证”:[{“类型”:“应用程序”,“类别”:“移动应用程序”,“子类别”:“其他”,“发行详情”:“虚拟”,“LMDT”:“2019-11-14 15:46:00.000”,“责任邮件”:alex@goba.com“}],“成功”:“正确”,“状态::“1”}
我无法使用下面的代码获取响应主体。如何从响应对象获取响应体?有人能帮我吗?。API调用Android代码如下:
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
Gson gson = new GsonBuilder()
.setLenient()
.create();
mRetrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
CryptocurrencyService scalarService = mRetrofit.create(CryptocurrencyService.class);
Call<String> stringCall = scalarService.getStringResponse("800028");
stringCall.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (response.isSuccessful()) {
String responseString = response.body().toString();
// todo: do something with the response string
Log.e("TAG_RESPONSE", responseString);
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
}
});
HttpLoggingInterceptor拦截器=新的HttpLoggingInterceptor();
拦截器.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient客户端=新建OkHttpClient.Builder().addInterceptor(拦截器).build();
Gson Gson=new GsonBuilder()
.setLenient()
.create();
mRetrofit=新改装.Builder()
.baseUrl(基本URL)
.客户(客户)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
CryptocurrencyService scalarService=mRetrofit.create(CryptocurrencyService.class);
Call stringCall=scalarService.getStringResponse(“800028”);
enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful()){
字符串responseString=response.body().toString();
//todo:对响应字符串执行一些操作
Log.e(“标签响应”,响应项);
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
}
});
问题在于您的响应不是JSON,这就是您无法获得响应的原因
相反,如果您的响应是一个普通字符串,那么您应该添加一个改型标量转换器com.squareup.改型2:converter scalars:2.5.0
,您可以看到一个示例
或者,如果希望将响应作为XML处理,则应添加此转换器compile'com.squareup.inversion2:converter jaxb:2.4.0