Reputation: 651
Actually I'm developing the app that send submit form as name-value pair using json for server communication . But the problem is now I want to send the Image or text file during form submission ,how shall I send the image or text file during form submission . Is there any correct procedure for this?
Upvotes: 0
Views: 260
Reputation: 7439
Try below code for Image Upload to server:
private class ImageUploader extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
String result = "";
// Client-side HTTP transport library
HttpClient httpClient = new DefaultHttpClient();
// using POST method
HttpPost httpPostRequest = new HttpPost(imagePostUrl);
try {
// creating a file body consisting of the file that we want to
// send to the server
FileBody bin = new FileBody(imageFile);
/**
* An HTTP entity is the majority of an HTTP request or
* response, consisting of some of the headers and the body, if
* present. It seems to be the entire request or response
* without the request or status line (although only certain
* header fields are considered part of the entity).
*
* */
MultipartEntityBuilder multiPartEntityBuilder = MultipartEntityBuilder.create();
multiPartEntityBuilder.addPart("images[1]", bin);
httpPostRequest.setEntity(multiPartEntityBuilder.build());
// Execute POST request to the given URL
HttpResponse httpResponse = null;
httpResponse = httpClient.execute(httpPostRequest);
// receive response as inputStream
InputStream inputStream = null;
inputStream = httpResponse.getEntity().getContent();
if (inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
return result;
} catch (Exception e) {
return null;
}
// return result;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
uploadStatus.setText("Uploading image to server");
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
uploadStatus.setText(result);
}
}
private static String convertInputStreamToString(InputStream inputStream)
throws IOException {
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream));
String line = "";
String result = "";
while ((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
Upvotes: 1
Reputation: 7439
For file try below code:
String url = "http://yourserver";
File file = new File(Environment.getExternalStorageDirectory(),
"yourfile");
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
InputStreamEntity reqEntity = new InputStreamEntity(
new FileInputStream(file), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true); // Send in multiple parts if needed
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
//Do something with response...
} catch (Exception e) {
// show error
}
Upvotes: 0