1

I am using a dummy rest API server called json-server. I wrote an android app which is able to make POSt request to the server, server returns an expected 201 as well.

I am not very familiar with REST api. I want to send a File from my device

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

Every time I do a post, a new Id is added under the Comments array on my json file located on the rest API server.

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

I am not sure how to change my android code or the REST API Post URl so I can send the whole text file content and it gets posted on the json file on the server.

7
  • 2
    localhost ? Is the server on the Android device too? Commented Apr 11, 2016 at 15:16
  • Other than the localhost problem, how to you know that the server supports file upload? Commented Apr 11, 2016 at 15:20
  • It's not localhost, updated the code. Can someone explain the downvote? I got to agree with the author here : medium.com/@johnslegers/… Commented Apr 11, 2016 at 15:22
  • Dont spend your time on this server. If you can read then you see that file upload is not supported. Commented Apr 11, 2016 at 15:27
  • Thanks for pointing that out. Can you please point me in the right direction, any generic approach on how to upload a file to the rest API server using the HTTP client ? Commented Apr 11, 2016 at 15:30

1 Answer 1

4

try this out

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

this code works perfectly, use this and if you find any difficulty, do comment.

 Happy coding!!
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Arpit, I tweaked your code a bit and it able to send my file now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.