0

I have this code:

HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(10000); conn.setConnectTimeout(10000); conn.setRequestMethod(type); conn.setDoOutput(output); if(output) conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setDoInput(true); conn.connect(); OutputStream out = null; if(output) out = conn.getOutputStream(); InputStream in = conn.getInputStream(); if(output) { BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out, "UTF-8")); bw.write(getQuery(params)); bw.flush(); } // Get output BufferedReader br = new BufferedReader(new InputStreamReader(in)); StringBuilder sb = new StringBuilder(); String line; while((line = br.readLine()) != null) { sb.append(line); sb.append("\n"); } in.close(); if(output) out.close(); // Do stuff with the output 

This code throws an IOException when output is true (doing a POST or PUT request), and doesn't complete the request. I've tried reordering the conn.connect(), write call, and stream creation blocks, but no permutation worked.

In all cases, the BufferedReader works just fine and doesn't throw any exceptions. getQuery correctly returns a URL-encoded string.

Here's the stack trace from the IOException:

java.io.IOException: closed W/System.err﹕ at com.android.okio.RealBufferedSink$1.write(RealBufferedSink.java:129) W/System.err﹕ at java.io.OutputStreamWriter.flushBytes(OutputStreamWriter.java:167) W/System.err﹕ at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:158) W/System.err﹕ at java.io.BufferedWriter.flush(BufferedWriter.java:124) W/System.err﹕ at org.project.NetworkHelper.doRequest(NetworkHelper.java:57) W/System.err﹕ at java.lang.Thread.run(Thread.java:818) 

What ends up happening is that the request goes through, but none of the POST or PUT data ends up at the server - it's like the write call never happened.

Does anyone have any idea what could cause this, or how to fix it?

1 Answer 1

2

You've complicated the code. Here's a sample for openConnection() implementation.

 byte[] postData = urlParameters.getBytes(Charset.forName("UTF-8")); int postDataLength = postData.length; JSONArray jsonArray = null; try { URL url = new URL(request); HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(); httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); httpURLConnection.setInstanceFollowRedirects(false); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); httpURLConnection.setRequestProperty("charset", "utf-8"); httpURLConnection.setRequestProperty("Content-Length", Integer.toString(postDataLength)); httpURLConnection.setConnectTimeout(10000); DataOutputStream dataOutputStream = new DataOutputStream( httpURLConnection.getOutputStream()); dataOutputStream.write(postData); dataOutputStream.flush(); dataOutputStream.close(); if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { InputStream responseStream = new BufferedInputStream( httpURLConnection.getInputStream()); BufferedReader responseStreamReader = new BufferedReader( new InputStreamReader(responseStream)); //get output } } catch (Exception e) { Customs.mToast(context, "Server down! Try after some time"); e.printStackTrace(); } 
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.