使用xutils發送POST請求,攜帶json和圖片二進制文件數據獲取服務器端返回json數據


接口文檔:

換頭像
接口 user/change_avatar

發送數據
HTTP Post body(一共2對KEY-VALUE):
json={"uid":"1","sid":"0123456789ABCDEF0123456789ABCDEF","ver":"1","request":{}}
file=圖片二進制文件數據

返回數據
{"ret":0,"response":{
    "status":1,
    "url":"http://192.168.1.200:8088/thumb.php?src=984340199_1667541218_1540991412.jpg&t=a&w=112&h=112"
    }
}

遇到的問題:
首先在封裝二進制請求體參數時,傳遞參數不足,導致服務器端不識別POST請求。
        HttpUtils http = new HttpUtils();
        RequestParams params = new RequestParams();
        params.addBodyParameter("json", json);
        for (int i = 0; i < files.size(); i++) {
            if (files.get(i) != null) {
                try {
                    params.addBodyParameter("file"+i, new FileInputStream(
                            files.get(i)), files.get(i).length(), files.get(i)
                            .getName(), "application/octet-stream");
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }

  params.addBodyParameter(key, stream, length, fileName, mimeType);

然后在發送POST請求時,使用的是異步方法,導致不能返回服務器返回的值。
        String s = null;
        try {
            // 同步方法,獲取服務器端返回的流
            ResponseStream responseStream = http.sendSync(HttpMethod.POST, url,
                    params);
            s = responseStream.readString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return s;

  ResponseStream responseStream = http.sendSync(method, url, params);

 下面附上發送POST請求的完整代碼:
    /**
     * 發送POST請求,攜帶json和多個文件參數得到服務器返回的結果(json格式),必須要開啟子線程調用。否則得不到數據。
     * 
     * @param url 請求地址
     * @param json 請求體中封裝的json字符串
     * @param List<File> 上傳多個文件
     * @return String 服務器返回的結果
     */
    public static String sendPost(String url, String json, List<File> files) {
        HttpUtils http = new HttpUtils();
        RequestParams params = new RequestParams();
        params.addBodyParameter("json", json);
        if (files.size() == 1) {
            try {
                params.addBodyParameter("file",
                        new FileInputStream(files.get(0)), files.get(0)
                                .length(), files.get(0).getName(),
                        "application/octet-stream");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        } else {
            for (int i = 0; i < files.size(); i++) {
                if (files.get(i) != null) {
                    try {
                        params.addBodyParameter("file" + i,
                                new FileInputStream(files.get(i)), files.get(i)
                                        .length(), files.get(i).getName(),
                                "application/octet-stream");
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        String s = null;
        try {
            // 同步方法,獲取服務器端返回的流
            ResponseStream responseStream = http.sendSync(HttpMethod.POST, url,
                    params);
            s = responseStream.readString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return s;
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM