将图像从 Android 上传到 PHP 服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2544517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:17:01  来源:igfitidea点击:

Uploading images to a PHP server from Android

androidfile-uploadhttpclient

提问by Samuh

I need to upload an image to a remote PHP server which expects the following parameters in HTTPPOST:

我需要将图像上传到远程 PHP 服务器,该服务器需要 HTTPPOST 中的以下参数:

*$_POST['title']*
*$_POST['caption']*
*$_FILES['fileatt']*

Most of the internet searches suggested either :

大多数互联网搜索都建议:

Download the following classes and trying MultiPartEntity to send the request:

下载以下类并尝试 MultiPartEntity 发送请求:

apache-mime4j-0.5.jar
httpclient-4.0-beta2.jar
httpcore-4.0-beta3.jar
httpmime-4.0-beta2.jar

OR

或者

Use URLconnection and handle multipart data myself.

使用 URLconnection 并自己处理多部分数据。

Btw, I am keen on using HttpClient class rather than java.net(or is it android.net) classes. Eventually, I downloaded the Multipart classes from the Android source code and used them in my project instead.

顺便说一句,我热衷于使用 HttpClient 类而不是 java.net(或者是 android.net)类。最后,我从 Android 源代码下载了 Multipart 类,并在我的项目中使用它们。

Though this can be done by any of the above mentioned methods, I'd like to make sure if these are the only ways to achieve the said objective. I skimmed through the documentation and found a FileEntityclass but I could not get it to work.

虽然这可以通过上述任何一种方法来完成,但我想确定这些是否是实现上述目标的唯一方法。我浏览了文档并找到了一个FileEntity类,但我无法让它工作。

What is the correct way to get this done in an Android application?

在 Android 应用程序中完成此操作的正确方法是什么?

Thanks.

谢谢。

采纳答案by Yoni Samlan

The Android source seems to come with an internal multipart helper library. See this post. At a quick glance, it's better documented than plenty of public APIs (cough cough, SoundPool, cough cough), so it should be a pretty good place to start, or possibly fine to just use a drop-in solution.

Android 源似乎带有一个内部多部分帮助程序库。看到这个帖子。乍一看,它比大量公共 API(咳嗽、SoundPool、咳嗽)有更好的文档记录,因此它应该是一个不错的起点,或者仅使用插入式解决方案可能没问题。

回答by znq

Maybe this poston the official Android group helps. The guy is using mime4j.

也许官方 Android 组上的这篇文章会有所帮助。这家伙正在使用 mime4j。

Another helpful resource could be this examplein the Pro Android book.

另一个有用的资源可能是Pro Android 书中的这个例子

回答by Rich

I do exactly that to interact with an image hosting server (implemented also in php) that expects parameters as if they were posted from an html page.

我这样做是为了与期望参数的图像托管服务器(也在 php 中实现)进行交互,就好像它们是从 html 页面发布的一样。

I build a List<NameValuePair>of my keys and values something like this:

我建立了一个List<NameValuePair>我的键和值是这样的:

    List<NameValuePair> params = new ArrayList<NameValuePair>(2);
    params.add(new BasicNameValuePair(key1, value1));
    params.add(new BasicNameValuePair(key2, value2));

and then I pass it to my http helper class that sets the HttpEntity property of my HttpPost request. Here's the method straight out of my helper class:

然后我将它传递给我的 http helper 类,该类设置了我的 HttpPost 请求的 HttpEntity 属性。这是直接来自我的助手类的方法:

public static HttpResponse Post(String url, List<NameValuePair> params, Context context)
{
    HttpResponse response = null;
    try
    {
        HttpPost request = new HttpPost();
        request.setURI(new URI(url));
        if(params != null)
            request.setEntity(new UrlEncodedFormEntity(params));
        HttpClient client = ((ApplicationEx)context.getApplicationContext()).getHttpClient();
        response = client.execute(request);
    }
    catch(Exception ex)
    {
        // log, etc
    }
    return response;
}