2015-04-05 200 views
0

我有这样的代码:HttpURLConnection的POST请求 - 不能将数据发送到服务器

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 

此代码抛出IOException时output是真实的(做一个POST或PUT请求),并没有完成请求。我尝试了重新排序conn.connect(),编写调用和流创建块,但没有任何排列工作。

在所有情况下,BufferedReader都可以正常工作,并且不会抛出任何异常。 getQuery正确返回一个URL编码的字符串。

下面是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) 

什么最终情况是,请求经过,但没有POST或PUT数据在服务器上结束了 - 这就像写调用从不发生了。

没有人有任何想法可能导致此,或如何解决呢?

回答

1

您已经复杂的代码。 下面是openConnection()实现的示例。

 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(); 
     }