android - 我想获取网页的信息为什么无法显示?
PHP中文网
PHP中文网 2017-04-17 17:49:44
0
3
451

package com.example.myroom.network;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

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 Activity implements View.OnClickListener {

public  static final int SHOW_RESPONSE=0;
private Button sendRequest;
private TextView responseText;
StringBuilder response=new StringBuilder();
private Handler handler=new Handler(){
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what){
            case SHOW_RESPONSE:
                String response=(String)msg.obj;
                responseText.setText(response);
        }
        super.handleMessage(msg);
    }
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sendRequest=(Button)this.findViewById(R.id.send_request);
    responseText=(TextView)this.findViewById(R.id.response);
    sendRequest.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    if(view.getId()==R.id.send_request){
        sendRequestWithHttpURLConnection();
        responseText.setText(response);
    }
}

private void sendRequestWithHttpURLConnection() {
    new Thread(){
        @Override
        public void run() {
            HttpURLConnection connection=null;
            try{

                URL url=new URL("http://www.baidu.com");
                connection=(HttpURLConnection)url.openConnection();
                connection.setRequestMethod("GET");
                connection.setConnectTimeout(8000);
                connection.setReadTimeout(8000);
                InputStream in=connection.getInputStream();
                BufferedReader reader=new BufferedReader(new InputStreamReader(in));

                String line;
                while((line=reader.readLine())!=null){
                    response.append(line);
                }
                Message message=new Message();
                message.what=SHOW_RESPONSE;
                message.obj=response.toString();
                handler.sendMessage(message);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if(connection!=null){
                    connection.disconnect();
                }
            }
            super.run();
        }
    }.start();
}

}

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

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button
    android:id="@+id/send_request"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Send request"/>
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical">

    <TextView
        android:id="@+id/response"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</ScrollView>

</LinearLayout>

有加权限,但不显示

PHP中文网
PHP中文网

认证高级PHP讲师

全員に返信(3)
黄舟

コードの最初の行をコピーしますか?

いいねを押す +0
阿神

ネットワーク権限を再度確認してください

ボタンをクリックしなかったか、シミュレーターがインターネットに接続されていません

いいねを押す +0
大家讲道理

あなたのコードに従って試してみましたが、うまくいきました

リーリー リーリー リーリー
いいねを押す +0
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!