Volley Request基类将两者定义为相同…
public class JacksonRequest<T> extends Request<T>...protected Response<T> parseNetworkResponse(NetworkResponse response)
这对我来说没什么意义.我无法想象许多REST请求对请求和响应使用相同的结构.
我错过了一些明显的东西吗?
解决方法 这是我的实施……public class JacksonRequest<T> extends JsonRequest<T>{ private Class<T> responseType; /** * Creates a new request. * * @param method * the http method to use * @param url * URL to fetch the JsON from * @param requestData * A {@link Object} to post and convert into Json as the request. Null is allowed and indicates no parameters will be posted along with request. * @param Listener * Listener to receive the JsON response * @param errorListener * Error Listener,or null to ignore errors. */ public JacksonRequest(int method,String url,Object requestData,Class<T> responseType,Listener<T> Listener,ErrorListener errorListener) { super(method,url,(requestData == null) ? null : Mapper.string(requestData),Listener,errorListener); this.responseType = responseType; } @OverrIDe protected Response<T> parseNetworkResponse(NetworkResponse response) { try { String JsonString = new String(response.data,httpheaderParser.parseCharset(response.headers)); return Response.success(Mapper.objectOrThrow(JsonString,responseType),httpheaderParser.parseCacheheaders(response)); } catch (Exception e) { return Response.error(new ParseError(e)); } }}
上面使用的Mapper类只是一个小包装类…
/** * Singleton wrapper class which configures the Jackson JsON parser. */public final class Mapper{ private static ObjectMapper MAPPER; public static ObjectMapper get() { if (MAPPER == null) { MAPPER = new ObjectMapper(); // This is useful for me in case I add new object propertIEs on the server sIDe which are not yet available on the clIEnt. MAPPER.configure(DeserializationConfig.Feature.FAIL_ON_UNKNowN_PROPERTIES,false); } return MAPPER; } public static String string(Object data) { try { return get().writeValueAsstring(data); } catch (Exception e) { e.printstacktrace(); return null; } } public static <T> T objectOrThrow(String data,Class<T> type) throws JsonParseException,JsonMapPingException,IOException { return get().readValue(data,type); } public static <T> T object(String data,Class<T> type) { try { return objectOrThrow(data,type); } catch (Exception e) { e.printstacktrace(); return null; } }}总结
以上是内存溢出为你收集整理的android – 在Volley中创建Jackson解析的请求全部内容,希望文章能够帮你解决android – 在Volley中创建Jackson解析的请求所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)