0

I have a file ABC(inputstream) that needs to upload from Android App over Jersey Java Rest Server.I am able to send the file alone but I have jsonObj(object converted to json ) along with file.

I am trying to convert inputstream into json String and then insert it into jsonObj. But no luck. Please guide me the way to approach this.


Update [1]

Java Client:

  1. with JSONObject

        ClientConfig config = new DefaultClientConfig();
        Client client = Client.create(config);
        WebResource service = client.resource(getBaseURI());
    
        JSONObject json = new JSONObject();
        json.put("id", "1213");
        json.put("name", "3");
        json.put("date", "2013-12-09");
        String input = new String( json.toString());
    
    ClientResponse response = service.path("/insert/").type(MediaType.APPLICATION_JSON).
                    post(ClientResponse.class,input);
    
  2. with inputStream

        String fileName="D://workspaces/src/readMe.pdf";
        InputStream fileInStream = new FileInputStream(fileName);
    
        ClientResponse response = service.path("/uploadFile").type(MediaType.APPLICATION_OCTET_STREAM)
                            .post(ClientResponse.class, fileInStream);
    

I hope Server code not required.

Now this work perfectly, But i want them together, i guess input stream can be merged with JSONObject, the question is how ?? some folks says that Jackson, but i need this client code on android application where i can not afford extra libs.


Navdeep Singh
  • 699
  • 1
  • 10
  • 25

1 Answers1

0

why you can not afford extra libs? Jackson is really nice for this, or Gson.

By the way, you can try the following:

InputStream inputStream = null;
String result = null;
try {
    InputStream fileInStream = new FileInputStream(fileName);

    // json is UTF-8 by default
    BufferedReader reader = new BufferedReader(new InputStreamReader(fileInStream , "UTF-8"), 8);
    StringBuilder sb = new StringBuilder();

    String line = null;
    while ((line = reader.readLine()) != null)
    {
        sb.append(line + "\n");
    }
    result = sb.toString();
} catch (Exception e) { 
    // handling
}
finally {
    try{
       if(inputStream != null){
           inputStream.close();}
    catch(Exception exc){}
}
Manu Zi
  • 2,300
  • 6
  • 33
  • 67
  • ok, then look at this: http://stackoverflow.com/questions/8726400/how-can-i-add-an-image-file-into-json-object – Manu Zi Jan 09 '14 at 09:54