HttpURLConnection 不工作

HttpURLConnection doesn't working

已建立 Internet 连接但未建立 URLconnection.URL 也是正确的。当我在 Web 浏览器中访问 URL 时,它会发送数据。请有人告诉我问题是什么?以及如何解决?

我的代码::

public class MainActivity extends AppCompatActivity {

private TextView text;

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

    Log.i("myinfo","start");
    text = (TextView)findViewById(R.id.text);
}

public void btnClicked(View v) {

    Log.i("myinfo","Btn clicked");
    new JSONTask().execute("jsonparsingdemo-cec5b.firebaseapp.com/jsonData/moviesDemoItem.txt");
}

public class JSONTask extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... params) {
        Log.i("myinfo","Do in back");
        HttpURLConnection connection = null;
        BufferedReader reader = null;

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

            Log.i("myinfo","Connection done");

            InputStream stream = connection.getInputStream();

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

            String line = "";
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }

            return buffer.toString();

        } catch (MalformedURLException e) {
            Log.i("myinfo","Connection not done");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                Log.i("myinfo","Connection disconnect");
                connection.disconnect();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        text.setText(result);
    }
}

}

输出流量::

04-29 11:55:51.558 32585-32585/com.smartrix.httpconection I/myinfo: start
04-29 11:55:54.403 32585-32585/com.smartrix.httpconection I/myinfo: Btn clicked
04-29 11:55:54.509 32585-32699/com.smartrix.httpconection I/myinfo: Do in back
04-29 11:55:54.509 32585-32699/com.smartrix.httpconection I/myinfo: Connection not done
 ....execute("jsonparsingdemo-cec5b......

您忘记了协议:

 ....execute("http://jsonparsingdemo-cec5b......

您的 url、

有问题
   URL url = new URL(params[0]);

使其成为字符串

url = new URL(urlString.toString());

希望它会起作用。