能接受到数值,但是手机界面不显示啊,求大神主代码:package com.wangy.baidudemo2;import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;import java.net.HttpURLConnection;
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;    private Handler handler = new Handler(){
       public void handleMessage(Message msg){
           switch(msg.what){
               case SHOW_RESPONSE:
                   System.out.println("run:===准备打印输出接收到的内容");//---运行时可打印此句
                   String resp = (String)msg.obj;
                   if(resp==null){
                       System.out.println("run:=====response中无内容内容===");
                   }else{
                       System.out.println("run:====response中有内容===");//---运行时可打印此句
                   }
                   System.out.println(resp);//---运行时打印无输出
                   responseText.setText(resp);
           }
       }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sendRequest = (Button)findViewById(R.id.send_request);
        responseText = (TextView)findViewById(R.id.response_text);
        sendRequest.setOnClickListener(this);
    }    @Override
    public void onClick(View v){
        if(v.getId()==R.id.send_request){
            sendResponseWithHttpURLConnection();
            System.out.println("run:===---点击了按钮---------");
        }
    }    public void sendResponseWithHttpURLConnection(){
        //开启线程来发起网络请求
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection connection = null;
                System.out.println("run:=====进入线程===");
                try{
                    URL url = new URL("http://www.baidu.com");
                    connection = (HttpURLConnection)url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(8000);
                    connection.setReadTimeout(8000);                    connection.setDoInput(true);
                    connection.connect();                    InputStream in = connection.getInputStream();
                    //下面对接收到的输入流进行读取
                    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                    StringBuilder responses = new StringBuilder();
                    String line;
                    while((line = reader.readLine())!=null){
                        responses.append(line);
                    }
                    System.out.println("run:====读取到内容===");
                    Message message = new Message();
                    message.what = SHOW_RESPONSE;
                    message.obj = responses.toString();
                    if(message.obj==null){
                        System.out.println("run:====obj中无内容内容===");
                    }else{
                        System.out.println("run:====obj中有内容===");//---运行时可打印此句
                    }
                    handler.sendMessage(message);
                }catch(Exception e){
                    e.printStackTrace();
                }finally{
                    if(connection != null)
                        connection.disconnect();
                    System.out.println("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"
    tools:context="com.wangy.baidudemo2.MainActivity">    <Button
        android:id="@+id/send_request"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="发送请求"/>
    <ScrollView
        android:layout_height="match_parent"
        android:layout_width="match_parent">        <TextView
            android:id="@+id/response_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
    </ScrollView></LinearLayout>

解决方案 »

  1.   

    AndroidManifest中已声明网络权限,点击手机界面的按钮就是无反应。求大神啊,多谢多谢
      

  2.   

    在线程里面打印一下  message.obj 试试
      

  3.   

    打印message.obj 没有任何东西啊,怎么办啊
      

  4.   

    System.out.println(resp);//---运行时打印无输出  这说明这个字符串内容为空啊, 你调试运行, 看到底有没有值啊.或者, 如果你觉得界面的代码有问题, 你可以强制给resp赋值一个固定的字符串, 看能否显示出来.
      

  5.   

    http://www.baidu.com改成https://www.baidu.com就行了,我也遇到了这个问题,改成http就好了。百度现在是全站https,所以你应该使用https://www.baidu.com来进行测试,但http://www.baidu.com在浏览器上又能访问到页面,是什么问题呢?这是因为百度做了跳转,自动给你跳转到https://www.baidu.com
      

  6.   

    https://www.baidu.com/