【问题标题】:Android, i cant read any web page easilyAndroid,我无法轻松阅读任何网页
【发布时间】:2013-09-14 06:44:37
【问题描述】:

我正在尝试以最简单的方式连接任何网页。我了解到我必须使用 AsyncTask,但我想在此之前看到这项工作。

package com.example.duzbaglanti;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {



    String biyazi = "ggg";
    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        tv = (TextView) findViewById(R.id.textView1);
        Button benimtus = (Button) findViewById(R.id.button1);

        benimtus.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                tv.setText(biyazi);
            }
        });



        HttpURLConnection benimconnection = null;
        try {
            URL benimurl = new URL("anypage");
            benimconnection = (HttpURLConnection) benimurl.openConnection();
            int sonuckodu = benimconnection.getResponseCode();
            if(sonuckodu == HttpURLConnection.HTTP_OK) {
                InputStream benimin = new BufferedInputStream(benimconnection.getInputStream());
                biyazi = benimin.toString();

                ///////
            }
        }
        catch (Exception e) {
            Log.d("baglantihatasi","hata oldu",e);
        }
        finally { benimconnection.disconnect(); }




    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

当我单击按钮时,我的字符串更改为 'Java.io.BufferedInputStream@4177b2a8' 而不是我的网页输出。怎么了?

【问题讨论】:

    标签: java android httpconnection


    【解决方案1】:

    你看过BufferedInputStream#toString()的实现吗?

    InputStream benimin = new BufferedInputStream(benimconnection.getInputStream());
    biyazi = benimin.toString();
    

    实际上没有。所以方法调用推迟到Object#toString(),因为这是第一个可用的实现的父类方法。就是这样实现的

    public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }
    

    换句话说,它与InputStream 的内容无关。您需要实际将流读入可能的字节数组,然后将其转换为StringHere's one way.

    【讨论】:

    • 在我的代码 InputStream benimin = new BufferedInputStream(benimconnection.getInputStream());
    • @user2778565 我的答案中的链接提供了有关如何将InputStream 的内容获取到String 的详细信息。请仔细阅读。
    【解决方案2】:

    检查你的清单文件,你是否在你的清单文件中添加了互联网权限,如果没有,添加它。然后就可以了!

    【讨论】:

      猜你喜欢
      • 2015-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-03
      • 1970-01-01
      • 1970-01-01
      • 2019-03-05
      • 2011-10-27
      相关资源
      最近更新 更多