1

我有一个向 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)
4

1 回答 1

14

查看源代码,当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 的扩展类扩展而来HttpUrlConnectionURLConnection。查看您发现的 javadocs:

受保护的布尔值输出

此变量由 setDoOutput 方法设置。它的值由 getDoOutput 方法返回。URL 连接可用于输入和/或输出。将 doOutput 标志设置为 true 表示应用程序打算将数据写入 URL 连接。

此字段的默认值为 false。

所以,你需要这样做:

httpConn.setDoOutput(true);

在你的代码中。

于 2012-12-24T23:09:08.413 回答