邮政与Android SDK的多部分请求 [英] Post multipart request with Android SDK

查看:180
本文介绍了邮政与Android SDK的多部分请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一些我以为会是比较简单:上传图片到了Android SDK的服务器。我发现了很多的例子code:

I'm trying to do something I thought would be relatively simple: Upload an image to a server with the Android SDK. I'm found a lot of example code:

<一个href="http://groups.google.com/group/android-developers/browse_thread/thread/f9e17bbaf50c5fc/46145fcacd450e48">http://groups.google.com/group/android-developers/browse_thread/thread/f9e17bbaf50c5fc/46145fcacd450e48

<一个href="http://linklens.blogspot.com/2009/06/android-multipart-upload.html">http://linklens.blogspot.com/2009/06/android-multipart-upload.html

但无论是为我工作。我依然会碰到的困惑是什么是真正需要做一个多部分请求。什么是的简单的办法有一个多部分上传(含图片)为Android?

But neither work for me. The confusion I keep running into is what is really needed to make a multipart request. What is the simplest way to have a multipart upload (with an image) for Android?

任何帮助或建议将不胜AP preciated!

Any help or advice would be greatly appreciated!

推荐答案

更新2014年4月29日:

我的回答是慈祥的老人​​现在,我想你更愿意使用一些高层次的库如改造

My answer is kind of old by now and I guess you rather want to use some kind of high level library such as Retrofit.

根据这个博客,我想出了以下解决方案: <一href="http://blog.tacticalnuclearstrike.com/2010/01/using-multipartentity-in-android-applications/">http://blog.tacticalnuclearstrike.com/2010/01/using-multipartentity-in-android-applications/

Based on this blog I came up with the following solution: http://blog.tacticalnuclearstrike.com/2010/01/using-multipartentity-in-android-applications/

您将需要下载额外的库来获得 MultipartEntity 运行!

You will have to download additional libraries to get MultipartEntity running!

1)从<一个下载httpcomponents-client-4.1.zip href="http://james.apache.org/download.cgi#Apache_Mime4J">http://james.apache.org/download.cgi#Apache_Mime4J并加入Apache的mime4j-0.6.1.jar到您的项目。

1) Download httpcomponents-client-4.1.zip from http://james.apache.org/download.cgi#Apache_Mime4J and add apache-mime4j-0.6.1.jar to your project.

2)从 http://hc.apache.org/downloads.cgi <下载httpcomponents-client-4.1-bin.zip /一>,并添加HttpClient的-4.1.jar,的HttpCore-4.1.jar和httpmime-4.1.jar到您的项目。

2) Download httpcomponents-client-4.1-bin.zip from http://hc.apache.org/downloads.cgi and add httpclient-4.1.jar, httpcore-4.1.jar and httpmime-4.1.jar to your project.

3)使用下面的例子中code。

3) Use the example code below.

private DefaultHttpClient mHttpClient;


public ServerCommunication() {
    HttpParams params = new BasicHttpParams();
    params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    mHttpClient = new DefaultHttpClient(params);
}


public void uploadUserPhoto(File image) {

    try {

        HttpPost httppost = new HttpPost("some url");

        MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);  
        multipartEntity.addPart("Title", new StringBody("Title"));
        multipartEntity.addPart("Nick", new StringBody("Nick"));
        multipartEntity.addPart("Email", new StringBody("Email"));
        multipartEntity.addPart("Description", new StringBody(Settings.SHARE.TEXT));
        multipartEntity.addPart("Image", new FileBody(image));
        httppost.setEntity(multipartEntity);

        mHttpClient.execute(httppost, new PhotoUploadResponseHandler());

    } catch (Exception e) {
        Log.e(ServerCommunication.class.getName(), e.getLocalizedMessage(), e);
    }
}

private class PhotoUploadResponseHandler implements ResponseHandler<Object> {

    @Override
    public Object handleResponse(HttpResponse response)
            throws ClientProtocolException, IOException {

        HttpEntity r_entity = response.getEntity();
        String responseString = EntityUtils.toString(r_entity);
        Log.d("UPLOAD", responseString);

        return null;
    }

}

这篇关于邮政与Android SDK的多部分请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆