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.