3

i am implementing one app related to send file to server.

i am trying to send file to server by using http post method.

i am getting file from sd card by using the fallowing code.

File root = Environment.getExternalStorageDirectory();
String pathToOurFile = root+"111";

My code is looking like as fallows.

StringBuilder response = new StringBuilder();
try {
  HttpPost post = new HttpPost();
  post.setURI(uri);
  List params = new ArrayList();
  params.add(new BasicNameValuePair("paramName", "paramValue"));
  post.setEntity(new UrlEncodedFormEntity(params));
  DefaultHttpClient httpClient = new DefaultHttpClient();
  HttpResponse httpResponse = httpClient.execute(post);
  if (httpResponse.getStatusLine().getStatusCode() == 200) {
    Log.d(APP_TAG, "HTTP POST succeeded");
    HttpEntity messageEntity = httpResponse.getEntity();
    InputStream is = messageEntity.getContent();
    BufferedReader br = new BufferedReader(new InputStreamReader(
    openFileInput(pathToOurFile)));
    String line;

    while ((line = br.readLine()) != null) {
     Log.v("info",",,,"+line);
     response.append(line);
     }
   } else {
     Log.e(APP_TAG, "HTTP POST status code is not 200");
   }
} catch (Exception e) {
  Log.e(APP_TAG, e.getMessage());
}

but it is not woking properly .

if know the solution please help me

Thanks in advance.

1 Answer 1

3

This should work, I havent tested the code though.

 import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.mime.MultipartEntity;
    import org.apache.http.entity.mime.content.FileBody;
    import org.apache.http.entity.mime.content.StringBody;
    import org.apache.http.impl.client.DefaultHttpClient;

    ...

        public static void uploadFile() throws Exception {
            HttpClient httpclient = new DefaultHttpClient();
            try {
                HttpPost httppost = new HttpPost(uri);
                String pathToOurFile = root+"111";

                File f = new File(pathToOurFile);
                FileBody bin = new FileBody(f);


                MultipartEntity reqEntity = new MultipartEntity();
                reqEntity.addPart("file", bin);
                reqEntity.addPart("paramName", paramValue);

                httppost.setEntity(reqEntity);

                HttpResponse response = httpclient.execute(httppost);
                HttpEntity resEntity = response.getEntity();

                String postResponse = response.getStatusLine();
        }

    }
Sign up to request clarification or add additional context in comments.

3 Comments

i am using the above code .but FileBody and MultipartEntity want to create a class.have u used any jar file here.Thanq for u'r reply
for other people who get here and need the libraries: hc.apache.org/downloads.cgi to download httpclient with the missing libraries, you can take just the httpmime jar from that library into your project
also getStatusLine gives you StatusLine which you then have to convert to string

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.