java HttpURLConnection 在 android 上返回垃圾

问题描述

如果我打开一个新线程并向 https://google.com 发出请求但返回垃圾

fetch function:
    public void fetch(View v) {
        fetchedData.clear();
        TextView label = findViewById(R.id.Hello_World_Label_1); // a label in the GUI
        Thread fetchThread = new Thread(new Customrunnable(v,label) {
            @Override
            public void run() {
                try {
                    URL url = new URL("https://instagram.com");//StrUrl);
                    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
                    conn.setSSLSocketFactory((SSLSocketFactory)HttpsURLConnection.getDefaultSSLSocketFactory());
                    conn.setHostnameVerifier(new HostnameVerifier() {
                        @Override
                        public boolean verify(String hostname,SSLSession session) {
                            return HttpsURLConnection.getDefaultHostnameVerifier().verify(hostname,session);
                        }
                    });
                    conn.connect();
                    BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
                    if (conn.getResponseCode() == 200) {
                        errorOccured = false;
                        byte[] barr = new byte[bis.available()];
                        bis.read(barr);
                        String[] str = (barr.toString()).split("\n");
                        fetchedData.addAll(Arrays.asList(str));
                    } else {
                        errorOccured = true;
                    }
                    bis.close();
                    conn.disconnect();
                } catch (java.net.MalformedURLException err) {
                    Log.d("ERROR_","MUE" + err.getMessage());
                } catch (java.io.IOException err) {
                    Log.d("ERROR_","IO: " + err.getMessage());
                }
            }
        });
        fetchThread.start();
    }

Customrunnable 类只是 Runnable 的包装器,只是覆盖了构造函数以便能够传递参数

我尝试在 https://instagram.com 上发出请求,但返回的垃圾大致相同。

对于最小的可执行示例,我添加了 get_line() 函数和 Customrunnable:

    public void get_line(View v) {
        TextView label = (TextView)findViewById(R.id.Hello_World_Label_1); // a label in the GUI
        if (fetchedData.size() != 0 && !errorOccured) {
            label.setText(fetchedData.get(0));
            fetchedData.remove(0);
        } else {
            label.setText("[No more data]");
        }
    }
public class Customrunnable implements Runnable {
    public Customrunnable(View view,TextView label_) {
        View v = view;
        TextView label = label_;
    }

    @Override
    public void run() {}
}

注意: MainActivity.java 中的一些变量:

private final List<Integer> grayButtons = new ArrayList<>();
private final List<String> fetchedData = new ArrayList<>();
private boolean errorOccured = false;

我是java和android开发的新手,所以请耐心等待

问题示例: 返回的数据如下所示:[B@c940d29,当我回忆起 get_line() 时,我到达了 [No more data]

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)