1

I want to upgrade my code for posting a json. The first one implements HttpClient and it works fine! I tried to use the new implementation using HttpURLconnection and it doesn't work! I can't send any post request. What I'm missing?

public class AsyncPostBG extends AsyncTask<String, String, String> { private ProxyState mData = null; private String mName = null; public AsyncPostBG(ProxyState data, String name) { mData = data; mName = name; } @Override protected String doInBackground(String... params){ HttpParams httpParams = new BasicHttpParams(); HttpClient httpClient = new DefaultHttpClient(httpParams); HttpPost postMethod = new HttpPost(params[0]); postMethod.setHeader("Content-Type", "application/json; charset=utf-8"); postMethod.setHeader("Accept", "*/*"); postMethod.setHeader("Accept-Encoding", "gzip, deflate"); Gson gson=new Gson(); String json = gson.toJson(mData); try { postMethod.setEntity(new ByteArrayEntity(json.getBytes("UTF8"))); HttpResponse response = httpClient.execute(postMethod); StatusLine statusLine = response.getStatusLine(); } catch (UnsupportedEncodingException e){ } catch (Exception e) { } return json; } } 

Here is my HttpURLConnection implementation:

private class AsyncStatePostBG extends AsyncTask<String, Void, String> { private ProxyObj mData = null; private String mName = null; public AsyncStatePostBG(ProxyObj data, String name) { mData = data; mName = name; } @Override protected String doInBackground(String... params) { Gson gson = new Gson(); String json = gson.toJson(mData); HttpURLConnection connection = null; try { URL object = new URL(ComURL + "api/state/" + mName); connection = (HttpURLConnection) object.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("Accept", "*/*"); connection.setRequestProperty("Accept-Encoding", "gzip, deflate"); connection.connect(); OutputStreamWriter streamWriter = new OutputStreamWriter(connection.getOutputStream()); streamWriter.write(json); streamWriter.flush(); streamWriter.close(); } catch (Exception exception) { return null; } return json; } } 
4
  • "it doesn't work" means exactly what? Commented Feb 1, 2016 at 8:31
  • I can't receive any request Commented Feb 1, 2016 at 8:32
  • mkyong.com/java/how-to-send-http-request-getpost-in-java this can help you. Commented Feb 1, 2016 at 8:35
  • 1
    You silently swallow all exceptions. However, they provide valuable input for diagnosing the problem. Commented Feb 1, 2016 at 8:35

1 Answer 1

1

You can just goahead with this code, it works for me..

 URL url = new URL("Your URL"); HttpURLConnection httpsURLConnection = (HttpURLConnection)url.openConnection(); httpsURLConnection.setReadTimeout(15000); httpsURLConnection.setConnectTimeout(20000); httpsURLConnection.setDoInput(true); httpsURLConnection.setRequestProperty("Content-Type", "application/json"); //Method Type httpsURLConnection.setRequestMethod("POST" : "PUT"); OutputStream outputStream = httpsURLConnection.getOutputStream(); BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8")); bufferedWriter.write("Your Params"); bufferedWriter.flush(); bufferedWriter.close(); outputStream.close(); httpsURLConnection.connect(); int mStatus = httpsURLConnection.getResponseCode(); 

Check the response code, and get the result like below

if (mStatus == 200 || mStatus == 201) return readResponse(httpsURLConnection.getInputStream()).toString(); 

Method for getting the response..

private static StringBuilder readResponse(InputStream inputStream) throws IOException, NullPointerException { BufferedReader r = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder stringBuilder = new StringBuilder(); String line; while ((line = r.readLine()) != null) { stringBuilder.append(line); } return stringBuilder; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.