Android:使用AsyncHttp POST上传图片



我有一个相机意图活动,我试图POST由用户选择的图像,到服务器。但每次它给我Java SSL套接字异常。下面是我试图实现的上传图像到服务器的方法。

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
            }
        });
    }

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);

我已经在下面解释了如何上传视频,同样可以用来上传图像。

您可以尝试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( )

相关内容

  • 没有找到相关文章

最新更新