首页 > 解决方案 > 使用带有 multipart 的 asyntask 上传图像不起作用?

问题描述

我想用一张图片发布三个字符串字段,我在下面的代码中尝试过它在没有图片的情况下工作

下面的方法我在 doInBackground 中调用 asyntask

private void fileUpload() {
    Log.e("file_upl","file_upl started");
    int serverResponse;
    Bitmap b = BitmapFactory.decodeFile("/sdcard/screen_shot.png");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    b.compress(Bitmap.CompressFormat.PNG, 0, baos);

    try {
        HttpClientcreaed client = new HttpClientcreaed(url+"/api/v1/screens/screenshot_upload/");

        HashMap<String,Object> hashMap_post = new HashMap<>();
        hashMap_post.put("device_id",deviceId);
        hashMap_post.put("random_number",randomNumber);
        hashMap_post.put("type","scrnshot");
        hashMap_post.put("file", baos.toByteArray()); //i think here 
                         // is problem how to sent image bye...without image its working

        serverResponse = client.connectForMultipart(hashMap_post);

        Log.e("checkingresponceresults","&&&&    "+serverResponse);
    }
    catch(Throwable t) {
        t.printStackTrace();
        Log.e("exception","excep " + t.toString());
    }
}

这是 我调用的Httpcliendcread 类中的方法

public int connectForMultipart(HashMap<String,Object> postDataParams) throws Exception {
        con = (HttpURLConnection) (new URL(url)).openConnection();
        con.setRequestMethod("POST");
        con.setDoInput(true);
        con.setDoOutput(true);
        con.setRequestProperty("Connection", "Keep-Alive"); //application/x-www-form-urlencoded
        //con.setRequestProperty("Content-Type", "multipart/x-www-form-urlencoded; boundary=" + boundary);
        con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        OutputStream os = con.getOutputStream();
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(os, "UTF-8"));
        writer.write(getPostDataString(postDataParams));
        writer.flush();
        writer.close();
        os.close();

        String response = "";
        if ( con.getResponseCode() == HttpsURLConnection.HTTP_OK) {
            String line;
            BufferedReader br=new BufferedReader(new InputStreamReader(con.getInputStream()));
            while ((line=br.readLine()) != null) {
                response+=line;
            }
        }
        Log.e("responchecking","&&&&   "+response);

        return con.getResponseCode();
        /*  con.connect();
        os = con.getOutputStream();*/
    }

这是附加 url 的另一种方法

private String getPostDataString(HashMap<String, Object> params) throws UnsupportedEncodingException {
        StringBuilder result = new StringBuilder();
        boolean first = true;
        for(Map.Entry<String, Object> entry : params.entrySet()){
            if (first)
                first = false;
            else
                result.append("&");

            result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
            result.append("=");
            result.append(URLEncoder.encode((String) entry.getValue(), "UTF-8"));


        }

        return result.toString();
    }

我已经搜索了谷歌,但我没有得到任何解决方案......

我检查了邮递员的工作

以下是错误:

例外 java.lang.ClassCastException: byte[] 不能转换为 java.lang.String

提前致谢

标签: androidandroid-asynctask

解决方案


推荐阅读