Reputation: 6632
I am trying to send data over to a rest
service using HttpUrlConnection
like this:
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
Upvotes: 1
Views: 572
Reputation: 310980
You need to flush()
the BufferedWriter
before you get the response code.
Upvotes: 2