I am creating an HttpsUrlConnection from a URL class. I am adding some header fields in the connection using the setRequestPropety method as demonstrated in following code.
URL url = new URL(address);
HttpURLsConnection connection = (HttpsURLConnection) url.openConnection();
connection.disconnect();
connection.setRequestProperty("Accept", "application/json");
I am getting exception, java.lang.IllegalStateException
: Cannot set request property after connection is made upon execution of setRequestMethod
. I even tried disconnection the connection as indicated in code above.
Thank you in advance.
NOTE: I also tried replacing setRequestProperty
with addRequestProperty
but no difference in exception.
NOTE: The stack trace is as below
10-27 13:53:40.352 29470-30420/com.MYCOM.po E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
10-27 13:53:40.352 29470-30420/com.MYCOM.po E/AndroidRuntime: Process: com.MYCOM.po, PID: 29470
10-27 13:53:40.352 29470-30420/com.MYCOM.po E/AndroidRuntime: java.lang.RuntimeException: An error occured while executing doInBackground()
10-27 13:53:40.352 29470-30420/com.MYCOM.po E/AndroidRuntime: at android.os.AsyncTask$3.done(AsyncTask.java:300)
10-27 13:53:40.352 29470-30420/com.MYCOM.po E/AndroidRuntime: at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
10-27 13:53:40.352 29470-30420/com.MYCOM.po E/AndroidRuntime: at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
10-27 13:53:40.352 29470-30420/com.MYCOM.po E/AndroidRuntime: at java.util.concurrent.FutureTask.run(FutureTask.java:242)
10-27 13:53:40.352 29470-30420/com.MYCOM.po E/AndroidRuntime: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
10-27 13:53:40.352 29470-30420/com.MYCOM.po E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
10-27 13:53:40.352 29470-30420/com.MYCOM.po E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
10-27 13:53:40.352 29470-30420/com.MYCOM.po E/AndroidRuntime: at java.lang.Thread.run(Thread.java:818)
10-27 13:53:40.352 29470-30420/com.MYCOM.po E/AndroidRuntime: Caused by: java.lang.IllegalStateException: Cannot set request property after connection is made
10-27 13:53:40.352 29470-30420/com.MYCOM.po E/AndroidRuntime: at com.android.okhttp.internal.http.HttpURLConnectionImpl.setRequestProperty(HttpURLConnectionImpl.java:496)
10-27 13:53:40.352 29470-30420/com.MYCOM.po E/AndroidRuntime: at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.setRequestProperty(DelegatingHttpsURLConnection.java:258)
10-27 13:53:40.352 29470-30420/com.MYCOM.po E/AndroidRuntime: at com.android.okhttp.internal.http.HttpsURLConnectionImpl.setRequestProperty(HttpsURLConnectionImpl.java:25)
10-27 13:53:40.352 29470-30420/com.MYCOM.po E/AndroidRuntime: at com.MYCOM.po.LoginActivity$UserLoginTask.doInBackground(LoginActivity.java:386)
10-27 13:53:40.352 29470-30420/com.MYCOM.po E/AndroidRuntime: at com.MYCOM.po.LoginActivity$UserLoginTask.doInBackground(LoginActivity.java:364)
NOTE: I am using the LoginActivity template, provided by Studio; to connect application to a RESTful web service.
The doInBackground method is as below
@Override
protected Boolean doInBackground(Void... params) {
try {
URL url = new URL(address);// Stored as a field in AsyncTask
HttpURLConnection connection;
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("REST-API-Key", "HEXKEY");
connection.setRequestProperty("Accept", "application/json");
StringBuilder paramBuilder = new StringBuilder();
paramBuilder.append("PARAM1=");
paramBuilder.append("VALUE1");
paramBuilder.append("&");
paramBuilder.append("PARAM2=VALUE2");
paramBuilder.append("&");
paramBuilder.append("PARAM3=");
paramBuilder.append("VALUE3");
connection.setDoOutput(true);
OutputStreamWriter outputWriter = new OutputStreamWriter(connection.getOutputStream());
outputWriter.write(paramBuilder.toString());
outputWriter.flush();
if(connection == null) {
Thread.sleep(2000);
return false;
}
BufferedReader responseReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
if(responseReader == null){
throw new IOException("Null Response");
}
StringBuilder sb = new StringBuilder();
String line;
while((line = responseReader.readLine())!=null){
sb.append(line+"\n");
}
String resp = sb.toString();
connection.disconnect();
} catch (InterruptedException|IOException e) {
return false;
}
// TODO: register the new account here.
return true;
}
Strange, But I removed all the break points, except the last one to check the result and It worked. I guess the delays,caused by breakpoints; was the problem. I still don't get it why it is getting connected even if connection.connect()
was not getting executed.
The solution was not to interrupt network operations.