0

I am trying to post data using multipart/form-data, but it is not working with httpPost method. I have to send data as json string from the API,but it is giving bad response from server if I remove content-type header, response says that do not ave permission to edit following data, I have to send 2 parameters from the API using POST,

below is my code:

public String webPostMultiForm(List<NameValuePair> params) {
        String postUrl = webServiceUrl;
        httpPost = new HttpPost(postUrl);
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(params));
            httpPost.addHeader("Cookie",appStatus.getSharedStringValue(appStatus.AUTH_KEY)); 
            httpPost.addHeader("Content-Type","multipart/form-data; boundary=assdsfdffafasf");
            httpPost.addHeader("Content-Disposition", "form-data; name= args");
            //httpPost.setHeader("Transfer-Encoding","chunked");
        } catch (UnsupportedEncodingException uee) {
            Log.e(TAG, uee.getMessage());
        }
        Log.e(TAG,"WebGetURL: " + postUrl);
        try {           
            response = httpClient.execute(httpPost);
        } catch (Exception e) {
            if (e.getMessage() != null) {
                Log.e(TAG, "httpClient.execute(httpPost) Exception: " + e.getMessage());
            } else {
                Log.e(TAG, "httpClient.execute(httpPost) Exception: " + e.getClass().toString());
            }
        }
        try {
            strResponse = EntityUtils.toString(response.getEntity());
        } catch (Exception e) {
            if (e.getMessage() != null) {
                Log.e(TAG, e.getMessage());
            } else {
                Log.e(TAG, e.getClass().toString());
            }
        }
        return strResponse;
    }

Is there anything I am doing wrong, or any other approach for this.

1

how about http://loopj.com/android-async-http/ using it. you don't have to worry for anything. Its very light weight.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.