gpt4 book ai didi

Java 安卓 OutputStreamWriter IOException

转载 作者:行者123 更新时间:2023-11-29 09:39:36 27 4
gpt4 key购买 nike

我有一个代码可以将请求发送到 php 页面并且它工作正常,但是突然(我不记得改变了什么)它在模拟器和真实设备上都停止工作

    URL url = new URL(this.site); 
URLConnection conn = url.openConnection();

if (!(conn instanceof HttpURLConnection)) {
Log.e(TAG, "connection is not HttpURLConnection");
throw new IOException("Not an HTTP connection");

}

try{
Log.e(TAG, "Trying");
HttpURLConnection httpConn = (HttpURLConnection) conn;
Log.e(TAG, "Created connection");
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("POST");
Log.e(TAG, "connecting");
httpConn.connect();
Log.e(TAG, "connected");

OutputStreamWriter wr = new OutputStreamWriter(httpConn.getOutputStream());
Log.e(TAG, "created outputstream");

这是堆栈跟踪:

     12-25 00:32:14.024: W/System.err(885): java.net.ProtocolException: Does not support output
12-25 00:32:14.090: W/System.err(885): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:575)
12-25 00:32:14.090: W/System.err(885): at com.*****.StringAsyncRetriever.OpenHttpConnection(StringAsyncRetriever.java:122)
12-25 00:32:14.090: W/System.err(885): at com.******.StringAsyncRetriever.getData(StringAsyncRetriever.java:66)
12-25 00:32:14.094: W/System.err(885): at com.******.StringAsyncRetriever.doInBackground(StringAsyncRetriever.java:38)
12-25 00:32:14.094: W/System.err(885): at com.******.StringAsyncRetriever.doInBackground(StringAsyncRetriever.java:1)
12-25 00:32:14.094: W/System.err(885): at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-25 00:32:14.094: W/System.err(885): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
12-25 00:32:14.094: W/System.err(885): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
12-25 00:32:14.094: W/System.err(885): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
12-25 00:32:14.094: W/System.err(885): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
12-25 00:32:14.094: W/System.err(885): at java.lang.Thread.run(Thread.java:1019)

最佳答案

查看源代码,当 doOutput 未设置为 true 时会抛出该异常:

(来自:http://www.java2s.com/Open-Source/Android/android-core/platform-libcore/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnectionImpl.java.htm)

public OutputStream getOutputStream() throws IOException {
if (!doOutput) {
throw new ProtocolException("Does not support output");
}

HttpURLConnectionImpl 类扩展自 Java 的 HttpUrlConnection 类,该类扩展了 URLConnection。查看您发现的 javadoc:

protected boolean doOutput

This variable is set by the setDoOutput method. Its value is returned by the getDoOutput method. A URL connection can be used for input and/or output. Setting the doOutput flag to true indicates that the application intends to write data to the URL connection.

The default value of this field is false.

所以,你需要做:

httpConn.setDoOutput(true);

在您的代码中。

关于Java 安卓 OutputStreamWriter IOException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14026133/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com