@Override public T convert(ResponseBody value) throws IOException { String returned = value.string(); try { return gson.fromJson(returned, type); } catch (JsonParseException e) { return (T) returned; } }
public T fromBody(ResponseBody value, Request request) throws IOException { String string = value.string(); System.out.println("网络请求到的字符串:" + string); Reader reader = new InputStreamReader((new ByteArrayInputStream(string.getBytes(UTF8))), Util.UTF_8); try { T t = typeAdapter.fromJson(reader); System.out.println("转换的最终对象:" + t); String mimeType = value.contentType().toString(); parseCache(request, t, string, mimeType); return t; } finally { closeQuietly(reader); } }
@Override public void onResponse(Response response) throws IOException { if (response != null && response.isSuccessful()) { ResponseBody body = response.body(); try { String result = body != null ? body.string() : null; ApiResult apiResult = onParseCallback(result); mResponsePoster.execute(new ResponseSuccessRunnable(apiResult)); } catch (JSONException e) { mResponsePoster.execute(new ResponseFailedRunnable("Parse error", e)); } catch (LoginException e) { mResponsePoster.execute(new ResponseFailedRunnable("Login error", e)); } catch (Exception e) { mResponsePoster.execute(new ResponseFailedRunnable("onParseCallback error", e)); } finally { if (body != null) body.close(); } } else { throw new IOException("Network error:Unexpected code " + response); } }
@Override public String fromBody(ResponseBody body) throws IOException { return body.string(); }