通过async-http-client上传文件给java服务器

由于刚入门,对public void uploadCertificate(org.springframework.ui.ModelMap result,javax.servlet.http.HttpServletRequest request)这种接口看不太懂。

后来才明白是要通过post传数据。首先我先把图片转成bitmap再转成二进制上传,结果报了数组越界。后来直接传文件,就没问题,汗。使用的asynchttpclient。

核心代码:

   RequestParams params = new RequestParams();
        File file =new File(filepath);
        try {
            params.put("profile_picture",file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        mClient.post(HttpConstant.Url_uploadCertificate,params,new BaseJsonHttpResponseHandler<Feedback<String>>() {
            @Override
            public void onSuccess(int i, Header[] headers, String s, Feedback<String> response) {
                if (sampleJsonResultListener != null) {
                    // 若response.getData()代表验证失败
                    if (response.getData() != null) {
                        sampleJsonResultListener.onSuccess(response);
                    } else {
                        sampleJsonResultListener.onFailure(response);
                    }
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, Throwable throwable, String s, Feedback<String> errorResponse) {
                if (sampleJsonResultListener != null) {
                    sampleJsonResultListener.onFailure(errorResponse);
                }
            }

            @Override
            protected Feedback parseResponse(String decodeJson, boolean isFailure) throws Throwable {
                if (!isFailure) {// 若请求成功从服务器返回则保证会有json数据
                    Feedback<String> resultStr = gson.fromJson(decodeJson, new TypeToken<Feedback<String>>() {
                    }.getType());
                    Log.i("uploadCertificate", decodeJson + "|" + resultStr);
                    return resultStr;
                }
                return null;
            }
        });

 

 

 

发布了28 篇原创文章 · 获赞 17 · 访问量 2万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章