求助。照搬《第一行代码-Android》的HttpURLConnection方法屏幕无显示?

记忆鎏金 2017-03-04 09:25:01
能接受到数值,但是手机界面不显示啊,求大神。。。

主代码:
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>

...全文
706 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
王能 2019-12-04
  • 打赏
  • 举报
回复
引用 11 楼 weixin_45953650 的回复:
没有用啊,修改了还是点击无法显示
禁止挖坟,请自己发帖,写清楚过程
  • 打赏
  • 举报
回复
没有用啊,修改了还是点击无法显示
键盘舞者113 2018-04-25
  • 打赏
  • 举报
回复
https://www.baidu.com/
jiandi7511 2018-04-25
  • 打赏
  • 举报
回复
引用 5 楼 zhetianyun 的回复:
http://www.baidu.com改成https://www.baidu.com就行了,我也遇到了这个问题,改成http就好了。百度现在是全站https,所以你应该使用https://www.baidu.com来进行测试,但http://www.baidu.com在浏览器上又能访问到页面,是什么问题呢?这是因为百度做了跳转,自动给你跳转到https://www.baidu.com
大佬,厉害了
qq_38293818 2017-11-21
  • 打赏
  • 举报
回复
引用 5 楼 zhetianyun 的回复:
http://www.baidu.com改成https://www.baidu.com就行了,我也遇到了这个问题,改成http就好了。百度现在是全站https,所以你应该使用https://www.baidu.com来进行测试,但http://www.baidu.com在浏览器上又能访问到页面,是什么问题呢?这是因为百度做了跳转,自动给你跳转到https://www.baidu.com
傲天鹰 2017-09-21
  • 打赏
  • 举报
回复
http://www.baidu.com改成https://www.baidu.com就行了,我也遇到了这个问题,改成http就好了。百度现在是全站https,所以你应该使用https://www.baidu.com来进行测试,但http://www.baidu.com在浏览器上又能访问到页面,是什么问题呢?这是因为百度做了跳转,自动给你跳转到https://www.baidu.com
xiaohuh421 2017-03-06
  • 打赏
  • 举报
回复
System.out.println(resp);//---运行时打印无输出 这说明这个字符串内容为空啊, 你调试运行, 看到底有没有值啊. 或者, 如果你觉得界面的代码有问题, 你可以强制给resp赋值一个固定的字符串, 看能否显示出来.
记忆鎏金 2017-03-05
  • 打赏
  • 举报
回复
引用 2 楼 vshuang 的回复:
在线程里面打印一下 message.obj 试试
打印message.obj 没有任何东西啊,怎么办啊
  • 打赏
  • 举报
回复
在线程里面打印一下 message.obj 试试
记忆鎏金 2017-03-04
  • 打赏
  • 举报
回复
AndroidManifest中已声明网络权限,点击手机界面的按钮就是无反应。求大神啊,多谢多谢

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧