使用Android HTTP客户端将文件上载到REST api服务器

使用Android HTTP客户端将文件上载到REST api服务器,android,rest,httpclient,Android,Rest,Httpclient,我正在使用一个名为的虚拟RESTAPI服务器。我编写了一个android应用程序,它可以向服务器发出POSt请求,服务器也会返回预期的201 我不太熟悉RESTAPI。我想从我的设备发送一个文件 public void run() { Logger.d("reponse","inside thread"); HttpClient client = new DefaultHttpClient(TunnelV

我正在使用一个名为的虚拟RESTAPI服务器。我编写了一个android应用程序,它可以向服务器发出POSt请求,服务器也会返回预期的201

我不太熟悉RESTAPI。我想从我的设备发送一个文件

     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();
                    }
                }
            }
每次我发表文章时,都会在RESTAPI服务器上json文件的注释数组下添加一个新Id

  "comments": [
{
  "id": 1,
  "body": "some comment",
  "postId": 1
},
{
  "id": 2
},]
我不知道如何更改我的android代码或RESTAPI Post URl,以便发送整个文本文件内容,并将其发布到服务器上的json文件中

试试这个

    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!!
试试这个

    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!!

localhost
?服务器也在安卓设备上吗?除了
localhost
问题,您如何知道服务器支持文件上传?不是localhost,更新了代码。有人能解释投票失败的原因吗?我同意作者的观点:不要把时间花在这台服务器上。如果你能阅读,那么你会发现文件上传是不受支持的。谢谢你指出这一点。您能为我指出正确的方向吗?关于如何使用HTTP客户端将文件上载到rest API服务器的任何通用方法?
localhost
?服务器也在安卓设备上吗?除了
localhost
问题,您如何知道服务器支持文件上传?不是localhost,更新了代码。有人能解释投票失败的原因吗?我同意作者的观点:不要把时间花在这台服务器上。如果你能阅读,那么你会发现文件上传是不受支持的。谢谢你指出这一点。你能为我指出正确的方向吗?关于如何使用HTTP客户端将文件上载到rest API服务器的任何通用方法?谢谢Arpit,我对你的代码进行了一些调整,现在它可以发送我的文件了。谢谢Arpit,我对你的代码进行了一些调整,现在它可以发送我的文件了。