微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java HttpURLConnection 在 android 上返回垃圾

如何解决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 举报,一经查实,本站将立刻删除。