【问题标题】:Android Studio no access to internetAndroid Studio 无法上网
【发布时间】:2020-11-21 18:06:25
【问题描述】:

我的应用程序访问一个简单的 JSON 并显示它在模拟器(PIXEL 2 API 24)上运行得非常好,但在我连接到它的任何实际设备上都没有。 S8 和 Note10 都试过了,都没有数据保护程序。

我什至后来检查了应用数据使用情况,它说“0 B”。

这里是文件:

MainActivity.java:

package com.example.internettest;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

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;

public class MainActivity extends AppCompatActivity {

    Button button;
    TextView textView;
    ProgressDialog pd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.button);
        textView = (TextView) findViewById(R.id.textView);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new JsonTask().execute("http://46d30cf268b0.ngrok.io/LEATest/");
            }
        });
    }

    private class JsonTask extends AsyncTask<String, String, String> {
        protected void onPreExecute() {
            super.onPreExecute();



            //pd = new ProgressDialog(MainActivity.this);
            //pd.setMessage("Please wait");
            //pd.setCancelable(false);
            //pd.show();
        }

        protected String doInBackground(String... params) {


            HttpURLConnection connection = null;
            BufferedReader reader = null;

            try {
                URL url = new URL(params[0]);
                connection = (HttpURLConnection) url.openConnection();
                connection.connect();


                InputStream stream = connection.getInputStream();

                reader = new BufferedReader(new InputStreamReader(stream));

                StringBuffer buffer = new StringBuffer();
                String line = "";

                while ((line = reader.readLine()) != null) {
                    buffer.append(line+"\n");
                    Log.d("Response: ", "> " + line);   //here u ll get whole response...... :-)
                }

                return buffer.toString();


            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (connection != null) {
                    connection.disconnect();
                }
                try {
                    if (reader != null) {
                        reader.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            //if (pd.isShowing()){
            //    pd.dismiss();
            //}
            Toast toast = Toast.makeText(getApplicationContext(),
                    "Before Try = |" + result + "|",
                    Toast.LENGTH_SHORT);

            toast.show();

            textView.setText(result);
        }
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.internettest">

    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

这是在模拟器上按下按钮的结果(所有假用户名/密码):

【问题讨论】:

    标签: java android json xml


    【解决方案1】:

    按如下方式执行您的 JsonTask -

    new JsonTask().execute("https://46d30cf268b0.ngrok.io/LEATest/");
    

    正如@isthemartin 所建议的,当您在实际设备(S8 和 Note10)中运行您的应用程序时,由于不允许 HTTP 连接,因此流量会被阻塞。

    您可以添加android:usesCleartextTraffic="true",但随后您的应用将允许非安全 HTTP 连接。

    我试过你的 API URL (https://46d30cf268b0.ngrok.io/LEATest/) 正在响应请求,为什么不简单地使用它而不在你的应用中造成安全漏洞。

    【讨论】:

      【解决方案2】:

      可能您的实际设备正在阻止 http 连接,因此在这种情况下,您需要启用这些连接,请查看post

      【讨论】:

        【解决方案3】:

        请将此代码添加到您的 AndroidManifest.xml 文件中。

           android:usesCleartextTraffic="true"
        

        所以代码可以是这样的:

         <?xml version="1.0" encoding="utf-8"?>
        <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.internettest">
        
        <uses-permission android:name="android.permission.INTERNET" />
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:usesCleartextTraffic="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
        
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
        

        【讨论】:

          猜你喜欢
          • 2018-05-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-10-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多