1

I'm trying to send a json object from my Android code to a webservice written in C# but could not invoke the webservice. I've setup a private network to which my system which is running webservice and android phone which is running android code are connected. To check whether webservice ever gets invoked, I installed a rest client on my phone and did a post, the break point in webservice got a hit. But when I try from my android code, I was able to setup a connection and when I write json object it waits as if it is writing. The breakpoint in webservice never gets a hit from android code. Below is my android code:


            Collection<JSONObject> items = new ArrayList<JSONObject>();
            JSONObject field = new JSONObject();
            String base64String = Base64.encodeToString(image_in_byte, 
            Base64.DEFAULT);

            File filename = new File(Directory1 + "/" + 
            file.getName());
            String file_str = file.getName();

            field.put("ID", file_str);
            field.put("Flag", "true");
            field.put("Image", base64String);
            field.put("Type", "JPEG" );
            items.add(field);

            UUID uid = UUID.randomUUID();
            String u_string = uid.toString();

            JSONObject temp = new JSONObject();
            temp.put("field1", u_string);
            temp.put("field2", "ABCD");
            temp.put("field3", items);

            HttpURLConnection rc;
            String SERVER = "webservice url";
            URL url = new URL(SERVER); 

            rc = (HttpURLConnection)url.openConnection();
            try
            {
                rc.setDoOutput(true);
                rc.setDoInput(true);
                rc.setUseCaches(false);
                rc.setRequestMethod("POST"); 
                rc.setRequestProperty("Connection", "Keep-Alive");
                rc.setRequestProperty("Content-Type", "application/json; charset=utf-8");
                rc.setRequestProperty("Accept", "application/json; charset=utf-8");
                rc.setConnectTimeout(timeout); 
                rc.setReadTimeout(0); 
                rc.connect();
                OutputStream out = new BufferedOutputStream(rc.getOutputStream());
                out.write(temp.toString().getBytes());

                out.flush();
                out.close();
               } 
            catch (Exception e) {
                e.printStackTrace();
            }
            finally
            {
                rc.disconnect();
            }

rc.connect() seems to be establishing the connection as the "connected" field in rc is true. temp above is the json object which I want to send to the webservice, the write operation waits, it seems to be writing but the webservice never gets a hit. I dont know why it is not working, any help in this direction is very much appreciated.

Thanks.

Ajay
  • 189
  • 1
  • 4
  • 16
  • Use Volley. [Here's](http://stackoverflow.com/questions/23220695/send-post-request-with-json-data-using-volley) an example. – Ruchira Randana Apr 23 '16 at 02:38
  • `temp above is the json file`. ??? Files have no toString() method. Please show relevant code. – greenapps Apr 23 '16 at 07:21
  • You have not the usual code in that catch block. So you are unaware of the things that happen. So you also cannot tell us. – greenapps Apr 23 '16 at 07:23
  • sorry, temp is not the json file.. it is a json object, my bad.. will put the relevant code. – Ajay Apr 23 '16 at 10:05
  • the code when executed after opening the connection doesnt generate any exception. when it does a write operation it waits for couple of seconds, executes out.flush and out.close successfully.. – Ajay Apr 23 '16 at 10:20
  • I used Volley and was able to invoke the webservice..Thanks Ruchira. – Ajay Apr 24 '16 at 10:44

0 Answers0