Cannot send GET or POST reply request to server, getting following error
java.net.ProtocolException: method does not support a request body: GET/POST
What I try to do is to read site content and calculate a simple math equation which is being generated in there and send answer back to server. Cannot figure out why it throws exception and how to fix my issue.
class HTTPRequest extends AsyncTask<String, String, String> {
String field1;
String field2;
public HTTPRequest (String arg1, String arg2) {
field1 = arg1;
field2 = arg2;
}
@Override
protected String doInBackground(String... urls) {
try {
URL url = new URL(urls[0]);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setChunkedStreamingMode(0);
InputStream in = new BufferedInputStream(conn.getInputStream());
String html = readStream(in);
Pattern pattern = Pattern.compile("\\t+(\\d+) ([+-]) (\\d+) = <input name=\"answer\"");
Matcher matcher = pattern.matcher(html);
Integer res = null;
if (matcher.find()) {
if (matcher.group(2).equals("-"))
res = Integer.parseInt(matcher.group(1)) - Integer.parseInt(matcher.group(3));
else if (matcher.group(2).equals("+"))
res = Integer.parseInt(matcher.group(1)) + Integer.parseInt(matcher.group(3));
}
//Result is calculated! sending POST request
if (res != null) {
Log.d("RESULT", Integer.toString(res));
String data = URLEncoder.encode("field1", "UTF-8")
+ "=" + URLEncoder.encode(field1, "UTF-8");
data += "&" + URLEncoder.encode("field2", "UTF-8") + "="
+ URLEncoder.encode(field2, "UTF-8");
data += "&" + URLEncoder.encode("answer", "UTF-8") + "="
+ URLEncoder.encode(Integer.toString(res), "UTF-8");
//ERR ON THIS LINE
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(data);
wr.flush();
wr.close();
BufferedReader in1 = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in1.readLine()) != null) {
response.append(inputLine);
}
in.close();
Log.d("RESPONE",respone);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private String readStream(InputStream is) throws IOException {
StringBuilder sb = new StringBuilder();
BufferedReader r = new BufferedReader(new InputStreamReader(is),1000);
for (String line = r.readLine(); line != null; line =r.readLine()){
sb.append(line);
sb.append(System.getProperty("line.separator"));
}
is.close();
return sb.toString();
}
}
traceback:
System.err: java.net.ProtocolException: method does not support a request body: POST
System.err: at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:201)
System.err: at com.streak.myapp.HTTPRequest.doInBackground(HTTPRequest.java:73)
System.err: at com.streak.myapp.HTTPRequest.doInBackground(HTTPRequest.java:24)
System.err: at android.os.AsyncTask$2.call(AsyncTask.java:287)
System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:234)
System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
System.err: at java.lang.Thread.run(Thread.java:841)