Android 来自改装的Json响应+;杰克逊转换器
我想知道在成功响应时是否可以将响应体作为字符串访问。在这里,我获得了成功:Android 来自改装的Json响应+;杰克逊转换器,android,jackson,retrofit,Android,Jackson,Retrofit,我想知道在成功响应时是否可以将响应体作为字符串访问。在这里,我获得了成功: serviceOne.editionsGet(Device, new Callback<Editions>() { @Override public void success(Editions editions, Response response) { //here i get my results
serviceOne.editionsGet(Device, new Callback<Editions>() {
@Override
public void success(Editions editions, Response response) {
//here i get my results
}
@Override
public void failure(RetrofitError error) {
showErrorToast();
}
});
serviceOne.editionsGet(设备,新回调(){
@凌驾
public void success(版本、响应){
//这是我的结果
}
@凌驾
公共无效失败(错误){
花洒吐司();
}
});
我想这可能是身体的反应。如果是如何从中获取字符串体。应该可以通过
response.getBody().toString()访问它。
InputStream is = response.getBody().in();
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
String body = sb.toString()
editions
应该是您自己实现的改装返回的对象
response
应该是改型返回的响应主体,它包含主体,可以使用getBody().toString()
方法作为字符串访问。使用JSONNode,您可以在响应中获得json
ApiInterface apiService =
ApiClient.getClient().create(ApiInterface.class);
Call<JsonNode> call = apiService.setDevice("application/json","eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdXRoX3Rva2VuIjoiMjY5NTUzNzIyOSIsImxvZ2luX2lkIjoiUlY1OTk3In0.NnPNRVUgaQnRBiza9yXM8FJf069Hb2ROtFw2mdlqK3w",rawJsonData());
call.enqueue(new Callback<JsonNode>() {
@Override
public void onResponse(Call<JsonNode>call, Response<JsonNode > response) {
Log.e("MAIN",response.body().toString());
/* Model model=response.body();
model.getstatus();*/
}
@Override
public void onFailure(Call<JsonNode>call, Throwable t) {
// Log error here since request failed
Log.e("MAIN", t.toString());
}
});
api接口apiService=
ApiClient.getClient().create(apinterface.class);
Call Call=apiService.setDevice(“application/json”,“eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyjhdxrox3rva2vuijoimjy5ntuzizosisimxvz2lux2lkijouly1otk3in0.nnpnpnrvugaqnrbiza9yxm8fjff69hb2rotfw2mdlq3w”,rawJsonData());
call.enqueue(新回调(){
@凌驾
public void onResponse(Callcall、Response-Response){
Log.e(“MAIN”,response.body().toString());
/*模型=response.body();
model.getstatus()*/
}
@凌驾
失败时公共无效(Callcall、Throwable t){
//由于请求失败,此处记录错误
Log.e(“MAIN”,t.toString());
}
});
就用这个
@POST("device")
Call<JsonNode> setDevice(@Header("Content-Type") String content_type, @Body Device parm);
@POST(“设备”)
调用setDevice(@Header(“Content Type”)字符串Content_Type,@Body Device parm);
使用上面的代码,我使用了Google Guava v11.0,下一个代码InputStream=null;尝试{is=response.getBody().in();}catch(IOException e){e.printStackTrace();}尝试{String text=CharStreams.toString(CharStreams.newReaderSupplier)((InputSupplierOops.Thinking)可以使用Guava进行转换。更新答案以仅使用标准的bufferedreader.TypedByteArray[length=287494],这是response.getBody().toString()的结果,但我需要json作为字符串