【问题标题】:Uploading file to a REST api server using Android HTTP client使用 Android HTTP 客户端将文件上传到 REST api 服务器
【发布时间】:2016-04-11 15:13:27
【问题描述】:

我正在使用一个名为json-server 的虚拟REST API 服务器。我编写了一个能够向服务器发出 POST 请求的 android 应用程序,服务器也返回预期的 201。

我对 REST api 不是很熟悉。我想从我的设备发送文件

     public void run() {
                    Logger.d("reponse","inside thread");

                    HttpClient client = new DefaultHttpClient(TunnelView.this);
                    String url = "http://some_server:3000/comments";
                    HttpGet get = new HttpGet(url);
                    HttpPost post = new HttpPost(url);
                    HttpResponse response = null;
                    post.setEntity(new FileEntity(new File("/root/sdcard/Download/File.txt"),"application/octect-stream"));
                    try
                    {
                        response = client.execute(post);
                        String res = response.getEntity().getContent().toString();
                        Logger.d("response", res);




                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            }

每次我发帖时,都会在我位于其余 API 服务器上的 json 文件的 Comments 数组下添加一个新 ID。

  "comments": [
{
  "id": 1,
  "body": "some comment",
  "postId": 1
},
{
  "id": 2
},]

我不确定如何更改我的 android 代码或 REST API Post URl,以便我可以发送整个文本文件内容并将其发布在服务器上的 json 文件中。

【问题讨论】:

  • localhost ?服务器也在安卓设备上吗?
  • 除了localhost的问题,你怎么知道服务器支持文件上传?
  • 不是本地主机,更新了代码。有人可以解释否决票吗?我在这里同意作者的观点:medium.com/@johnslegers/…
  • 不要把时间花在这台服务器上。如果您可以阅读,那么您会看到不支持文件上传。
  • 感谢您指出这一点。您能否指出正确的方向,关于如何使用 HTTP 客户端将文件上传到其余 API 服务器的任何通用方法?

标签: android rest httpclient


【解决方案1】:

试试这个

    public static JSONObject postFile(String url,String filePath,int id){
    String result="";
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    File file = new File(filePath);
    MultipartEntity mpEntity = new MultipartEntity();
    ContentBody cbFile = new FileBody(file, "image/jpeg");
    StringBody stringBody= null;
    JSONObject responseObject=null;
    try {
        stringBody = new StringBody(id+"");
        mpEntity.addPart("file", cbFile);
        mpEntity.addPart("id",stringBody);
        httpPost.setEntity(mpEntity);
        System.out.println("executing request " + httpPost.getRequestLine());
        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity resEntity = response.getEntity();
        result=resEntity.toString();
        responseObject=new JSONObject(result);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return responseObject;
}

此代码完美运行,使用此代码,如果您发现任何困难,请发表评论。

 Happy coding!!

【讨论】:

  • 感谢 Arpit,我稍微调整了您的代码,它现在可以发送我的文件了。
猜你喜欢
  • 2011-05-31
  • 2011-01-28
  • 2014-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多