try using this method where strJsonRequest is the json string you want to publish and strUrl is the URL you want to send strJsonRequest to
public String urlPost(String strJsonRequest, String strURL) throws Exception { try { URL objURL = new URL(strURL); connection = (HttpURLConnection)objURL.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setAllowUserInteraction(false); connection.setUseCaches(false); connection.setConnectTimeout(TIMEOUT_CONNECT_MILLIS); connection.setReadTimeout(TIMEOUT_READ_MILLIS); connection.setRequestMethod("POST"); connection.setRequestProperty("Accept-Charset", "utf-8"); connection.setRequestProperty("Content-Type", "application/json; charset=utf-8"); connection.setRequestProperty("Content-Length", ""+strJsonRequest.toString().getBytes("UTF8").length); DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream()); byte [] b = strJsonRequest.getBytes("UTF-8"); outputStream.write(b); outputStream.flush(); inputstreamObj = (InputStream) connection.getContent();//getInputStream(); if(inputstreamObj != null) strResponse = convertStreamToString(inputstreamObj); } catch(Exception e) { throw e; } return strResponse; }
and the convertStreamToString () method is below
private static String convertStreamToString(InputStream is) { BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(is)); } catch (Exception e1) {
G_s
source share