Android : 使用 AsyncHttp 上传图片 POST
Android : To upload an image using AsyncHttp POST
我有相机意图 activity,我正在尝试 POST 将用户选择的图像发送到服务器。但每次它都给我 Java SSL Socket Exception 。
下面是我尝试实现的将图片上传到服务器的方法。
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
}
});
}
我已经在下面解释了如何上传视频,同样可以用来上传图片。
您可以尝试HttpClient jar下载最新的HttpClient jar,将其添加到您的项目中,然后使用以下方法上传视频:
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( )
AsyncHttpClient
支持上传文件。但是你应该这样使用它:
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);
我有相机意图 activity,我正在尝试 POST 将用户选择的图像发送到服务器。但每次它都给我 Java SSL Socket Exception 。 下面是我尝试实现的将图片上传到服务器的方法。
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
}
});
}
我已经在下面解释了如何上传视频,同样可以用来上传图片。
您可以尝试HttpClient jar下载最新的HttpClient jar,将其添加到您的项目中,然后使用以下方法上传视频:
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( )
AsyncHttpClient
支持上传文件。但是你应该这样使用它:
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);