2014-05-05 38 views
1

我嘗試過使用HttpUrlConnection這樣的rest服務發送數據:發送數據超過使用HttpURLConnection的

public void makePutRequest(String objtype,String objkey,String json) 
{ 
    String uri="http://localhost:8180/GoogleMapsLoadingTest/rest/GoogleMapsErp/"+objtype+"/"+objkey; 
    HttpURLConnection conn=null; 
    URL url=null; 
    BufferedWriter writer=null; 
    try { 
     url=new URL(uri); 
    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    try { 
     conn=(HttpURLConnection) url.openConnection(); 
     conn.addRequestProperty("Content-Type", "application/json"); 
     conn.setRequestMethod("PUT"); 
     conn.setDoInput(true); 
     conn.setDoOutput(true); 
     System.out.println(json); 
     writer=new BufferedWriter(new OutputStreamWriter(conn.getOutputStream())); 
     writer.write(json); 
     int rc=conn.getResponseCode(); 
     System.out.println("The response code is "+rc); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    finally 
    { 
     if(writer!=null) 
     { 
      try { 
       writer.flush(); 
       writer.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      if(conn!=null) 
       conn.disconnect(); 
     } 
    } 

} 

I get this error because the String could not be sent over

回答

2

您需要flush()BufferedWriter你得到的迴應代碼之前。

相關問題