自定义Requet

Activity代码:

Volley 网络通信框架 * 完全异步 * 怎么实现呢 

* 1:因为Volley是并发的跟Handler里面消息机制一样 

* 消息队列管理 消息 * RequestQueue 管理 Request(StringRequest,ImageRequest)

 * 最后不要忘记add进去 *

* Volley请求图片有三种方式 * 1:ImageRequest * 2:ImageLoader * 3:NetWorkImageView * 自定义Request怎么写? * 通过参考StringRequest * private final ListenermListener; 用来保存数据的回调接口

* deliverResponse()方法成功过后的处理

* parseNetworkResponse()方法请求中的处理

*

* Gson gson = new Gson();

*

* gson.fromJson(jsonStr,T.class); 

* type java===>T * ArryList

//  自定义Request    

private void getZDYRequest() {       

 RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);        MyGsonRequestmyGson = new MyGsonRequest<>(Request.Method.GET, mUrl, null, 

MyData.class, new Response.Listener() {          

  @Override          

  public void onResponse(MyData response) {          

      Toast.makeText(MainActivity.this, response.getMessage(), Toast.LENGTH_SHORT).show();            }       

 });     

   requestQueue.add(myGson);    }

public class MyGsonRequestextends Request{   

 private Response.ListenermListener;   

 private Gson mGson;   

 private ClassmClass;   

 public MyGsonRequest(int method, String url, Response.ErrorListener listener, Classcls, 

Response.ListenersucessList) {        

super(method, url, listener);       

 this.mListener = sucessList;        

this.mClass = cls;       

 mGson = new Gson();    }

//自定义Request    //网络处理    

@Override   

 protected ResponseparseNetworkResponse(NetworkResponse response) {

String jsonStr;

try {

jsonStr = new String(response.data, HttpHeaderParser.parseCharset(response.headers));

} catch (UnsupportedEncodingException e) {

jsonStr = new String(response.data);

}

return Response.success(mGson.fromJson(jsonStr, mClass), HttpHeaderParser.parseCacheHeaders(response));

}

//成功过后

@Override

protected void deliverResponse(T response) {

mListener.onResponse(response);

}

}




阅读更多

Springmvc.MultipartResolver异常:Failed to perform multipart cleanup servlet requet

11-25

最近使用SpringMVC写了一个文件上传,使用的是CommonMultiResolver ,在其中设置了最大值500k,懒加载之后,文件上传符合要求可以使用rn[code=html]rn rn rn rn rn rn rnrn rn rn[/code]rn[code=java]rn//对于文件上传异常的处理(针对文件大于上传限制)rn @ExceptionHandler(Exception.class)rn public ModelAndView handleException(Exception ex,HttpServletRequest request ) rn ModelAndView model = new ModelAndView("view");rnrn if (ex instanceof MaxUploadSizeExceededException) rnrn model.addObject("errors", "文件应不大于 " + getFileKB(((MaxUploadSizeExceededException) ex).getMaxUploadSize()));rnrn else rn model.addObject("errors", "不知错误: " + ex.getMessage());rn rn return model;rn rn[/code]rnrn再这样进行异常处理之后,在测试超过500k的文件时,跳转不了页面,而且后台报错rn[code=text]rn WARN - mmons.CommonsMultipartResolver - Failed to perform multipart cleanup for servlet request rnorg.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size of 512000 bytes exceeded; nested exception is org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (17774939) exceeds the configured maximum (512000)rn at org.springframework.web.multipart.commons.CommonsMultipartResolver.parseRequest(CommonsMultipartResolver.java:160)rn at org.springframework.web.multipart.commons.CommonsMultipartResolver$1.initializeMultipart(CommonsMultipartResolver.java:131)rn at org.springframework.web.multipart.support.AbstractMultipartHttpServletRequest.getMultipartFiles(AbstractMultipartHttpServletRequest.java:119)rn at org.springframework.web.multipart.support.AbstractMultipartHttpServletRequest.getMultiFileMap(AbstractMultipartHttpServletRequest.java:99)rn at org.springframework.web.multipart.commons.CommonsMultipartResolver.cleanupMultipart(CommonsMultipartResolver.java:188)rn at org.springframework.web.servlet.DispatcherServlet.cleanupMultipart(DispatcherServlet.java:1034)rn at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:981)rn at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)rn at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)rn at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)rn at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)rn at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)rn at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)rn at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)rn at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)rn at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)rn at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)rn at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)rn at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)rn at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)rn at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)rn at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)rn at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)rn at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2522)rn at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2511)rn at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)rn at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)rn at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)rn at java.lang.Thread.run(Thread.java:745)rnCaused by: org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (17774939) exceeds the configured maximum (512000)rn at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.(FileUploadBase.java:965)rn at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:310)rn at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:334)rn at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:115)rn at org.springframework.web.multipart.commons.CommonsMultipartResolver.parseRequest(CommonsMultipartResolver.java:156)rn ... 36 morern[/code]

没有更多推荐了,返回首页