Android 无法在AsyncTask内创建outputStream

Android 无法在AsyncTask内创建outputStream,android,android-asynctask,Android,Android Asynctask,在异步任务中我要发布数据: @Override protected Void doInBackground(Void... params) { ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connect

异步任务中
我要发布数据:

@Override
        protected Void doInBackground(Void... params) {

            ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

            if (networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected()) {

                //System.setProperty("http.keepAlive", "false");

                HttpURLConnection con = null;
                OutputStream os = null;
                OutputStreamWriter writer = null;

                try {
                    String donnees = URLEncoder.encode("param1","UTF-8").concat("=").concat(URLEncoder.encode("valeur1","UTF-8"));
                    String strUrl = confUrl.concat("outil.php?action=OutilImporterDonneesMobile");
                    URL url = new URL(strUrl);
                    con = (HttpURLConnection)url.openConnection();
                    con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
                    con.setDoOutput(true);
                    con.setChunkedStreamingMode(0);
                    if (con != null && con.getResponseCode() == HttpURLConnection.HTTP_OK) {
                        try {
                            os = con.getOutputStream();
                            if (os != null) {
                                writer = new OutputStreamWriter(os);
                                if (writer != null) {
                                    writer.write(donnees);
                                    writer.flush();
                                }
                            }
                        } catch (IOException e) {
                            error = true;
                            err = "cannot create outputstream";
                        }
                    } else {
                        error = true;
                        err = contextInsideSync.getResources().getString(R.string.errBadHTTPResponse);
                    }
                } catch (MalformedURLException e) {
                    error = true;
                    err = contextInsideSync.getResources().getString(R.string.errBadUrl);
                } catch (UnsupportedEncodingException e) {
                    error = true;
                    err = contextInsideSync.getResources().getString(R.string.errEncodageUTF8);
                } catch (IOException e) {
                    error = true;
                    err = contextInsideSync.getResources().getString(R.string.errAccessError);
                } catch (Exception eo) {
                    error = true;
                    err = eo.getMessage();
                }
                finally {
                    if (writer != null) { try { writer.close(); } catch (IOException e) {
                        error = true;
                        err = contextInsideSync.getResources().getString(R.string.errAccessError);
                    } }
                    if (os != null) { try { os.close(); } catch (IOException e1) {
                        error = true;
                        err = contextInsideSync.getResources().getString(R.string.errAccessError);
                    } }
                    if (con != null) con.disconnect();
                }

            } else {
                error = true;
                err = contextInsideSync.getResources().getString(R.string.errNoNetwork);
            }

            return null;

        }

在运行时,尝试创建
outputStream
时会引发
IOException
。有什么问题吗?

这里的问题是,您正在发送HTTP头,然后写入输出

这一行:

con.getResponseCode()
…您实际上已经要求发送请求


在调用getResponseCode()之前,请尝试打开OutputStream并写入它

请发布异常java.net.ProtocolException:在读取响应后无法写入请求正文事实上
getResponseCode()
不是必需的吗?我会调用getResponseCode(),但在您完成构建请求之后。您可以/应该使用它来确定请求是否成功。