HttpURLConnection GET方法仅在调试时在api 22中工作

时间:2015-05-15 10:22:35

标签: android get httpurlconnection

HttpURLConnection GET方法仅在调试模式下在api 22中工作,而在其他api中它可以正常工作。有时它可以工作,但在大约4个案例中的3个案例中它会引发异常:

E/Http GET HttpURLConnection:﹕ 2015/05/15 13:18:04 failed to connect to /10.254.254.1 (port 80) after 5000ms

这是我的代码:

try
    {
        URL url = new URL(path);
        urlConnection = (HttpURLConnection)  url.openConnection();
        urlConnection.setInstanceFollowRedirects(false);
        urlConnection.setConnectTimeout(5000);
        urlConnection.setReadTimeout(5000);
        urlConnection.setRequestMethod("GET");
        urlConnection.setRequestProperty("User-Agent", USER_AGENT);
        urlConnection.setRequestProperty("Content-Type", "text/plain;charset=utf-8");


        int responseCode = urlConnection.getResponseCode();

        mFunctions.AddLog(3, "Http GET responseCode: ", Integer.toString(responseCode));


        BufferedReader in = new BufferedReader(
                new InputStreamReader(urlConnection.getInputStream()));
        String inputLine = in.readLine();

        return inputLine;

    } catch (MalformedURLException e)
    {
        mFunctions.AddLog(3, "Http GET URL: ", e);
    } catch (IOException e)
    {
        mFunctions.AddLog(3, "Http GET HttpURLConnection: ", e);
    }
    finally
    {
        urlConnection.disconnect();
    }

我尝试使用HttpOk代替HttpUrlConnection,这次问题在api 22上是相同的,我得到另一个例外:

6371-6611/kepardvpn.client W/System.err﹕ java.net.SocketTimeoutException: failed to connect to /10.254.254.1 (port 80) after 15000ms
05-15 20:05:52.364    6371-6611/kepardvpn.client W/System.err﹕ at libcore.io.IoBridge.connectErrno(IoBridge.java:169)
05-15 20:05:52.364    6371-6611/kepardvpn.client W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:122)
05-15 20:05:52.364    6371-6611/kepardvpn.client W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
05-15 20:05:52.364    6371-6611/kepardvpn.client W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
05-15 20:05:52.364    6371-6611/kepardvpn.client W/System.err﹕ at java.net.Socket.connect(Socket.java:882)
05-15 20:05:52.364    6371-6611/kepardvpn.client W/System.err﹕ at com.squareup.okhttp.internal.Platform$Android.connectSocket(Platform.java:197)
05-15 20:05:52.364    6371-6611/kepardvpn.client W/System.err﹕ at com.squareup.okhttp.Connection.connect(Connection.java:156)
05-15 20:05:52.364    6371-6611/kepardvpn.client W/System.err﹕ at com.squareup.okhttp.Connection.connectAndSetOwner(Connection.java:175)
05-15 20:05:52.364    6371-6611/kepardvpn.client W/System.err﹕ at com.squareup.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:120)
05-15 20:05:52.364    6371-6611/kepardvpn.client W/System.err﹕ at com.squareup.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:330)
05-15 20:05:52.364    6371-6611/kepardvpn.client W/System.err﹕ at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:319)
05-15 20:05:52.364    6371-6611/kepardvpn.client W/System.err﹕ at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:241)
05-15 20:05:52.364    6371-6611/kepardvpn.client W/System.err﹕ at com.squareup.okhttp.Call.getResponse(Call.java:271)
05-15 20:05:52.364    6371-6611/kepardvpn.client W/System.err﹕ at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:228)
05-15 20:05:52.364    6371-6611/kepardvpn.client W/System.err﹕ at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:199)
05-15 20:05:52.364    6371-6611/kepardvpn.client W/System.err﹕ at com.squareup.okhttp.Call.execute(Call.java:79)
05-15 20:05:52.364    6371-6611/kepardvpn.client W/System.err﹕ at kepardvpn.client.HttpConnections.get(HttpConnections.java:196)
05-15 20:05:52.364    6371-6611/kepardvpn.client W/System.err﹕ at kepardvpn.client.Functions$GetCurrentIPTask.doInBackground(Functions.java:1549)
05-15 20:05:52.364    6371-6611/kepardvpn.client W/System.err﹕ at kepardvpn.client.Functions$GetCurrentIPTask.doInBackground(Functions.java:1537)
05-15 20:05:52.364    6371-6611/kepardvpn.client W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:292)
05-15 20:05:52.364    6371-6611/kepardvpn.client W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-15 20:05:52.365    6371-6611/kepardvpn.client W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
05-15 20:05:52.365    6371-6611/kepardvpn.client W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-15 20:05:52.365    6371-6611/kepardvpn.client W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-15 20:05:52.365    6371-6611/kepardvpn.client W/System.err﹕ at java.lang.Thread.run(Thread.java:818)

这是我的代码:

    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(15, TimeUnit.SECONDS); // connect timeout
    client.setReadTimeout(30, TimeUnit.SECONDS);    // socket timeout
    client.networkInterceptors().add(new UserAgentInterceptor(USER_AGENT));

    Request request = new Request.Builder()
            .url(path)
            .build();

    Response response = null;
    try
    {
        response = client.newCall(request).execute();


        InputStream in = null;

        in = response.body().byteStream();

        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String result, line = reader.readLine();
        //TimeUnit.MILLISECONDS.sleep(2000);
        result = line;
        while ((line = reader.readLine()) != null)
        {
            result += line;
        }

        if (response.code() == 200)
            return result;
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }

1 个答案:

答案 0 :(得分:0)

试试这个

public static String retrieveStream(String link, String parameters) {
        try {
            link = link.replace(" ", "%20");
            // parameters = parameters.replace(" ", "%20");
            URL url = new URL(link);

            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");

            connection.setRequestMethod("POST");

            OutputStreamWriter request = new OutputStreamWriter(
                    connection.getOutputStream());
            request.write(parameters);
            request.flush();
            request.close();

            String line = "";
            InputStreamReader isr = new InputStreamReader(
                    connection.getInputStream());
            BufferedReader reader = new BufferedReader(isr);
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            // Response from server after login process will be stored in
            // response variable.
            String response = sb.toString();
            // You can perform UI operations here
            isr.close();
            reader.close();
            return response;

        } catch (IOException ignored) {
            ignored.printStackTrace();
        }
        return "";
    }