通過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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章