0

I want to post three string fields with one image i have tried below code its working without images

Below method i calling asyntask in doInBackground

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

This is the method is inside Httpcliendcread class i calling

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

This is another method for append the 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();
    }

I have searched google but i didnot getting any solution...

I checked postman its working

below is error:

excep java.lang.ClassCastException: byte[] cannot be cast to java.lang.String

Thanks in advance

demo
  • 672
  • 3
  • 9
  • 34
  • You are trying to send byte array of image as string and hence getting exception, here is a sample solution, https://stackoverflow.com/a/27579944/2520628 – Tejashwi Kalp Taru Aug 08 '18 at 12:29
  • i konw using retrofit and volley how to sent two text files one images....but using asyntask i donot know...please anyone help me – demo Aug 08 '18 at 12:29
  • @TejashwiKalpTaru i already check that link...in that link only one image uploading...i want to sent two param as a string one param as images sir... – demo Aug 08 '18 at 12:31
  • check this article, with source code: https://www.survivingwithandroid.com/2013/05/android-http-downlod-upload-multipart.html – Tejashwi Kalp Taru Aug 08 '18 at 12:33
  • @TejashwiKalpTaru ya i already checked sir...`httpclient` is deprecated from marshmellow its not working.it working 4.4 and 5.0..i tested sir – demo Aug 08 '18 at 12:36
  • @TejashwiKalpTaru That artical is created 2014 sir – demo Aug 08 '18 at 12:40
  • What about this one? https://stackoverflow.com/a/34409142/2520628 – Tejashwi Kalp Taru Aug 08 '18 at 12:42

0 Answers0