android http URL connection在使用https时有效,在使用http时无效。

huangapple 未分类评论49阅读模式
英文:

android http URL connection working with https, not working with http

问题

以下是翻译好的代码部分:

我尝试使用以下代码执行http GET请求对于https URL字符串它能够正常工作但对于http URL则无法正常工作

我不明白为什么会这样会抛出一个IOException异常
你能帮忙吗我尝试检索相同的URL例如Google使用https和不使用https后者可以正常工作问题是什么我已经在清单文件中添加了用户访问互联网的权限

package com.example.httptest;
import android.os.AsyncTask;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

class GetData extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        HttpURLConnection urlConnection = null;
        String result = "";
        try {
            //URL url = new URL("https://www.fineco.it");
            URL url = new URL("https://www.google.com");
            Log.e("MAU","0");
            urlConnection = (HttpURLConnection) url.openConnection();
            Log.e("MAU","1");
            int code = urlConnection.getResponseCode();
            Log.e("MAU","2");

            if (code == 200) {
                InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                if (in != null) {
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
                    String line = "";

                    while ((line = bufferedReader.readLine()) != null)
                        result += line;
                }
                in.close();
            }
            Log.e("MAU",result);

            return result;
        } catch (MalformedURLException e) {
            Log.e("MAU","Malformed URL");

            e.printStackTrace();
        } catch (IOException e) {
            Log.e("MAU","IOException");

            e.printStackTrace();
        } finally {
            urlConnection.disconnect();
        }
        return result;
    }
}

请注意,这只是你提供的代码的翻译部分,没有其他额外的内容。如果你需要进一步的帮助或解释,请随时提问。

英文:

I have tried to perform an http GET by using the following code. It works perfectly with https URL string, instead with http url doesn't work.

I Do not understand why. An IOException is given.
Can you help? I have tried to retrieve the same URL, for instance google, with and without, https, the latter is working. What's the issue? I have added in the manifest the users permission to access internet.

package com.example.httptest;
import android.os.AsyncTask;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
class GetData extends AsyncTask&lt;String, Void, String&gt; {

@Override
protected String doInBackground(String... params) {
    HttpURLConnection urlConnection = null;
    String result = &quot;&quot;;
    try {
        //URL url = new URL(&quot;https://www.fineco.it&quot;);
        URL url = new URL(&quot;https://www.google.com&quot;);
        Log.e(&quot;MAU&quot;,&quot;0&quot;);
        urlConnection = (HttpURLConnection) url.openConnection();
        Log.e(&quot;MAU&quot;,&quot;1&quot;);
        int code = urlConnection.getResponseCode();
        Log.e(&quot;MAU&quot;,&quot;2&quot;);

        if (code == 200) {
            InputStream in = new BufferedInputStream(urlConnection.getInputStream());
            if (in != null) {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
                String line = &quot;&quot;;

                while ((line = bufferedReader.readLine()) != null)
                    result += line;
            }
            in.close();
        }
        Log.e(&quot;MAU&quot;,result);

        return result;
    } catch (MalformedURLException e) {
        Log.e(&quot;MAU&quot;,&quot;Malformed URL&quot;);

        e.printStackTrace();
    } catch (IOException e) {
        Log.e(&quot;MAU&quot;,&quot;IOException&quot;);

        e.printStackTrace();
    } finally {
        urlConnection.disconnect();
    }
    return result;
}

}

答案1

得分: 0

将以下行添加到您的androidManifest.xml文件的application标签中,然后尝试进行调试:

android:usesCleartextTraffic="true"
英文:

Add this lines to your androidManifest.xml in application tag & try debugging it

android:usesCleartextTraffic=&quot;true&quot;

答案2

得分: 0

我找到了这篇帖子,解决了这个问题:
https://medium.com/mindorks/my-network-requests-are-not-working-in-android-pie-7c7a31e33330

英文:

I managed to find this post which solved the issue
https://medium.com/mindorks/my-network-requests-are-not-working-in-android-pie-7c7a31e33330

huangapple
  • 本文由 发表于 2020年5月5日 02:21:45
  • 转载请务必保留本文链接:https://java.coder-hub.com/61599009.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定