URLConnection - 读取响应后无法写入请求主体

问题描述:

我有一组对象,我试图通过API发布。第一个对象将通过它应该,然后我得到:URLConnection - 读取响应后无法写入请求主体

java.net.ProtocolException: cannot write request body after response has been read 

我相当肯定,它只是在我做事情的顺序,但我仍然在学习Android和一直在努力找出一个小时,现在一点。

想到我会转向你们寻求帮助!我需要接收对POST的每个响应,所以我可以将它设置为稍后使用的ID字段。你会看到我在我的FOR循环中解析它,然后将ID存储在一个列表中,我将稍后循环。

代码下面的POST:

try { 
      url = new URL("##Edited Out##"); 
      conn = (HttpsURLConnection) url.openConnection(); 

      // Create the SSL connection 
      sc = SSLContext.getInstance("TLS"); 
      sc.init(null, null, new SecureRandom()); 
      conn.setSSLSocketFactory(sc.getSocketFactory()); 

      // Use this if you need SSL authentication 
      String userpass = "##Edited Out##:##Edited Out##"; 
      String basicAuth = "Basic " + Base64.encodeToString(userpass.getBytes(), Base64.DEFAULT); 
      conn.setRequestProperty("Authorization", basicAuth); 
      conn.setRequestProperty("Content-Type", "application/json;odata=verbose"); 

      // set Timeout and method 
      conn.setReadTimeout(7000); 
      conn.setConnectTimeout(7000); 
      conn.setRequestMethod("POST"); 
      conn.setDoInput(true); 
      conn.setDoOutput(true); 

      conn.connect(); 

      JSONObject juo = new JSONObject(); 

      for (int i = 0; i<objects.size(); i++){ 

       juo.put("Field1", "Data for field 1 here"); 
       juo.put("Field2", "Data for field 2 here"); 
       juo.put("Field3", "Data for field 3 here"); 

       wr = new OutputStreamWriter(conn.getOutputStream()); 
       wr.write(juo.toString()); 
       wr.flush(); 
       wr.close(); 

       Log.d("ITEM STRING", juo.toString()); 

       BufferedReader br = new BufferedReader(new InputStreamReader(
         conn.getInputStream(), "utf-8")); 
       while ((line = br.readLine()) != null) { 
        sb.append(line + "\n"); 
       } 
       br.close(); 

       Log.d("HTTP BR", "" + sb.toString()); 

       jObject = XML.toJSONObject(sb.toString()); 

       Log.d("JSON OBJECT", jObject.toString()); 

       JSONObject menuObject = jObject.getJSONObject("entry"); 
       JSONObject contentObject = menuObject.getJSONObject("content"); 
       JSONObject propertiesObject = contentObject.getJSONObject("m:properties"); 
       JSONObject productObject = propertiesObject.getJSONObject("d:Product_Id"); 
       String retProId = productObject.getString("content"); 

       partIDs.add(retProId); 

       Log.d("PART ID", retProId); 
      } 

      int HttpResult = conn.getResponseCode(); 
      Log.d("HTTP RESULT", String.valueOf(HttpResult)); 
      Log.d("HTTP RESPONSE", conn.getResponseMessage()); 

     } catch (NoSuchAlgorithmException e) { 
      e.printStackTrace(); 
     } catch (KeyManagementException e) { 
      e.printStackTrace(); 
     } catch (ProtocolException e) { 
      e.printStackTrace(); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return null; 

编辑:下面 关于重复的评论我收到的错误是稍有不同。我理解它的原理,但我的.getRespondeCode不在我的for循环中,它应该循环。但事实并非如此。

此错误发生,如果objects.size> = 2

我需要获得服务器响应字符串为每个对象发布为它含有一个ID我需要解析。

+1

的可能的复制[读取输入后不能写输出(http://*.com/questions/11413028/cannot-write-output-after-reading-input) – e4c5

HTTP协议基于请求 - 响应模式:您首先发送请求并且服务器响应。一旦服务器响应,你不能发送更多的内容,这是没有道理的。

看看你的代码,你会得到for循环中的响应。当访问一个响应时,下一次循环抛出异常。如果你想创建多个请求,那么应该每次创建一个新的urlconnection。例如。

private void requestMethod(){ 
     //your for loop goes here 

    } 

    private void backgroundOperation(/**required parameter*/){ 
     // your httpurlconnection code goes here don't use for loop. use finally block to close connection also 
    } 
+0

感谢你为这个。我以一种稍微不同的方式做到了这一点,但这让我走上了正轨! – Noodelz