我正在从URL类创建一个HttpsUrlConnection。我正在使用setRequestPropety方法在连接中添加一些头字段,如下面的代码所示。
URL url = new URL(address);
HttpURLsConnection connection = (HttpsURLConnection) url.openConnection();
connection.disconnect();
connection.setRequestProperty("Accept", "application/json");
我收到异常,java.lang.IllegalStateException
:在执行setRequestMethod
连接后无法设置请求属性。我甚至尝试断开连接,如上面的代码所示。
提前谢谢。
注意:我也尝试将setRequestProperty
替换为addRequestProperty
,但异常没有区别。
注意:堆栈跟踪如下
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)
注意:我正在使用Studio提供的LoginActivity模板;将应用程序连接到RESTful Web服务。
doInBackground方法如下
@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;
}
答案 0 :(得分:1)
奇怪,但我删除了所有的断点,除了最后一个断点以检查结果并且它有效。我想是由断点造成的延误;是问题。即使connection.connect()
没有被执行,我仍然无法理解为什么它会连接起来。
解决方案不是中断网络操作。
答案 1 :(得分:-1)
请从代码中删除connection.disconnect()
。
典型的使用模式是:
HttpUrlConnection ucon = (HttpUrlConnection) url.openConnection();
ucon.setRequestProperty("name", "value");
...
InputStream is = null;
try {
is = ucon.getInputStream();
// read inputstream. ideally wrapping it in a BufferedReader.
} finally {
if (is != null) is.close();
}
答案 2 :(得分:-1)
您应该在断开连接之前设置标头。
URL url = new URL(address);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Accept", "application/json");
try {
InputStream in = new BufferedInputStream(connection.getInputStream());
readStream(in);
finally {
connection.disconnect();
}