使用带有multipart的asyntask的图像上传不起作用?

时间:2018-08-08 12:24:21

标签: android android-asynctask

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

在下面的方法中,我在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();*/
    }

这是附加网址的另一种方法

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();
    }

我已经搜索过google,但没有得到任何解决方案...

我检查了邮递员的工作

下面是错误:

  

excep java.lang.ClassCastException:byte []无法转换为   java.lang.String

预先感谢

0 个答案:

没有答案