java.lang.IllegalStateException:已经连接

时间:2018-02-20 08:01:33

标签: android http-post httpurlconnection illegalstateexception

我使用标题库授权发布我的服务。(网址没问题)但是我遇到了这个问题。这项服务在邮递员工作正常,但在我的代码不工作。这是邮政服务。我更改了所有连接功能,如urlConnection.setDoOutput,但我总是连接已连接。

StringBuilder sb = new StringBuilder();
            HttpURLConnection urlConnection = null;
            try {
                URL url = new URL(getUrl.trim());
                urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setDoInput(true);
                urlConnection.setDoOutput(true);
                urlConnection.setRequestMethod("POST");
                urlConnection.setUseCaches(false);
                urlConnection .getResponseCode();
                String userPassword = userName + ":" + pwd;
                byte[] data = userPassword.getBytes("UTF-8");
                String base64UPWD = Base64.encodeToString(data, Base64.DEFAULT);
                urlConnection.setRequestProperty("Authorization", "Basic " + base64UPWD);
                params.put("token", getToken());
                StringBuffer requestParams = new StringBuffer();
                Iterator<String> paramIterator = params.keySet().iterator();
                while (paramIterator.hasNext()) {
                    String key = paramIterator.next();
                    String value = params.get(key);

                    requestParams.append(key).append("=");
                    requestParams.append(URLEncoder.encode(value, "UTF-8"));

                    requestParams.append("&");
                }
                System.out.println("Req Param: " + requestParams.toString());

                urlConnection.setFixedLengthStreamingMode(requestParams.toString().getBytes().length);
                urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());
                out.write(requestParams.toString());
                out.close();

                int HttpResult = urlConnection.getResponseCode();
                if (HttpResult == HttpURLConnection.HTTP_OK) {
                    BufferedReader br = new BufferedReader(new InputStreamReader(
                            urlConnection.getInputStream(), "utf-8"));
                    String line = null;
                    while ((line = br.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    br.close();
                    getResult = sb.toString();
                    Log.i("Request Result-->>", getResult);
                    isSuccess = true;
                    System.out.println("Response: " + sb.toString());

                } else {
                    System.out.println(urlConnection.getResponseMessage());
                }


            } catch (UnknownHostException ex) {
                ex.printStackTrace();
                exception = "unknownhost";

            } catch (MalformedURLException e) {
                e.printStackTrace();
                exception = "clientprotocol";

            } catch (SocketTimeoutException e) {
                e.printStackTrace();
                exception = "sockettimeout";

            } catch (IOException e) {
                e.printStackTrace();
                exception = "connectiontimeout";
            } catch (NullPointerException e) {
                e.printStackTrace();
                exception = "nullpointer";

            } catch (Exception e) {
                e.printStackTrace();
                exception = e.toString();

            } finally {
                if (urlConnection != null)
                    urlConnection.disconnect();
            }

0 个答案:

没有答案