Reputation: 490
I am having a camera intent activity , I am trying to POST an image selected by the user , to the server. But every time it is giving me Java SSL Socket Exception . Below is the method which i have tried to implement for uploading the image to the server .
private void postImage(String url) {
Context context = this.getApplicationContext();
File file = new File(getFilePath());
MimeTypeMap map = MimeTypeMap.getSingleton();
String ext = FilenameUtils.getExtension(file.getName());
String mime_type = map.getMimeTypeFromExtension(ext);
MultipartEntity form = new MultipartEntity();
form.addPart("files[]", new FileBody(file, mime_type, "UTF-8"));
AsyncHttpClient client = new AsyncHttpClient();
client.post(context, url, form, mime_type, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject jsonObject1) {
// called when response HTTP status is "200 OK"
if (statusCode == 200) {
try {
String url = jsonObject1.getString("imageUrl");
String blobkey = jsonObject1.getString("blobKey");
Log.d(TAG, "IMAGE URL : " + url + " \n BlobKey : " + blobkey + " ");
} catch (JSONException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable e, JSONObject jsonObject1) {
Log.d(TAG, "Status Code : " + statusCode);
}
@Override
public void onRetry(int retryNo) {
// called when request is retried
}
});
}
Upvotes: 1
Views: 1258
Reputation: 14021
AsyncHttpClient
supports uploading files. But you should use it like this:
RequestParams params= new RequestParams();
//params.put("file", new File(filePath));
params.put("file", new File(filePath), contentType);
AsyncHttpClient client = new AsyncHttpClient();
//client.post(url, params, listener);
client.put(url, params, listener);
Upvotes: 1
Reputation: 1005
I have explained how to upload video here below, Same can be used to upload image.
You can try HttpClient jar download the latest HttpClient jar, add it to your project, and upload the video using the following method:
private void uploadVideo(String videoPath) throws ParseException, IOException {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(YOUR_URL);
FileBody filebodyVideo = new FileBody(new File(videoPath));
StringBody title = new StringBody("Filename: " + videoPath);
StringBody description = new StringBody("This is a video of the agent");
StringBody code = new StringBody(realtorCodeStr);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("videoFile", filebodyVideo);
reqEntity.addPart("title", title);
reqEntity.addPart("description", description);
reqEntity.addPart("code", code);
httppost.setEntity(reqEntity);
// DEBUG
System.out.println( "executing request " + httppost.getRequestLine( ) );
HttpResponse response = httpclient.execute( httppost );
HttpEntity resEntity = response.getEntity( );
// DEBUG
System.out.println( response.getStatusLine( ) );
if (resEntity != null) {
System.out.println( EntityUtils.toString( resEntity ) );
} // end if
if (resEntity != null) {
resEntity.consumeContent( );
} // end if
httpclient.getConnectionManager( ).shutdown( );
} // end of uploadVideo( )
Upvotes: 0