0

I am able to post string values to PHP server by using the following code:

 public void callWebService(String strEmailList){
    HttpResponse response = null;
    String responseBody="";
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(6);
    nameValuePairs.add(new BasicNameValuePair("stringkey1",
            String_Value1));
    nameValuePairs.add(new BasicNameValuePair("stringkey2", String_Value2));
    nameValuePairs.add(new BasicNameValuePair("stringkey3", String_Value3));
    nameValuePairs.add(new BasicNameValuePair("stringkey4", String_Value4));
    nameValuePairs.add(new BasicNameValuePair("stringkey5", String_Value5));
    nameValuePairs.add(new BasicNameValuePair("stringkey6", Here i need to post Image));

    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://MY URL");

        if (nameValuePairs != null)
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        response = httpclient.execute(httppost);
        responseBody = EntityUtils.toString(response.getEntity());
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    handleResponse(responseBody);
}

I am getting responseBody perfectly if i post only string values. In the nameValuePair, I need to post Image to Server. Can anyone help me how to post image using following code.

1

3 Answers 3

4

You can send image to the server as a Multipart entity

public void upload(String filepath) throws IOException
    {
     HttpClient httpclient = new DefaultHttpClient();
     httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
     HttpPost httppost = new HttpPost("url");
     File file = new File(filepath);
     MultipartEntity mpEntity = new MultipartEntity();
     ContentBody cbFile = new FileBody(file, "image/jpeg");
     mpEntity.addPart("userfile", cbFile); 
     httppost.setEntity(mpEntity);
     System.out.println("executing request " + httppost.getRequestLine());
     HttpResponse response = httpclient.execute(httppost);
     HttpEntity resEntity = response.getEntity();
             // check the response and do what is required
      }
Sign up to request clarification or add additional context in comments.

Comments

0

For uploading image and Video,,, you need to use MultiPart.First you need to Attach your file in fileBody which later attach in Multipart

public JSONObject file_upload1(String URL, String userid, String topic_id,
        String topicname, String filelist, String taglist,
        String textComment, String textLink) {
    JSONObject jObj = null;
    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(URL);

        FileBody bin = null;
        MultipartEntity reqEntity = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);

            File file = new File(filelist);

            try {
                bin = new FileBody(file);
            } catch (Exception e) {
                e.printStackTrace();
            }
            reqEntity.addPart("post_data" + i, bin);

        reqEntity.addPart("tag", new StringBody("savetopicactivities"));
        reqEntity.addPart("user_id", new StringBody(userid));
        reqEntity.addPart("text", new StringBody(textComment));
        reqEntity.addPart("count",
                new StringBody(String.valueOf(taglist.size())));
        reqEntity.addPart("topic_id", new StringBody(topic_id));
        reqEntity.addPart("topic_name", new StringBody(topicname));
        reqEntity.addPart("link", new StringBody(textLink));

        httpPost.setEntity(reqEntity);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        json = sb.toString();
        System.out.println("json   " + json);
        try {
            jObj = new JSONObject(json);
        } catch (Exception e) {

            e.printStackTrace();
        }

        is.close();

    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // return JSON String
    return jObj;
}

Comments

0
    HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    HttpPost httpPost = new HttpPost(url);

    try {
        MultipartEntity entity = new  MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

        for (int index = 0; index < nameValuePairs.size(); index++) 
        {
            if (index == nameValuePairs.size()-1) 
            {
                entity.addPart(nameValuePairs.get(index).getName(),
                        new FileBody(new File(nameValuePairs.get(index)
                                .getValue())));
            } else {

                    entity.addPart(nameValuePairs.get(index).getName() , new                                        StringBody(nameValuePairs.get(index).getValue()));
                }


            }

        httpPost.setEntity(entity);
        HttpResponse response = httpClient.execute(httpPost, localContext);
        HttpEntity resEntity = response.getEntity();
        if (resEntity != null) 
        {
            String resdata = EntityUtils.toString(resEntity);
            System.out.println("DATA :" + resdata);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

1 Comment

add httpmime-4.2.1.jar into your project

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.