第一行代码 网络请求部分按照书本来仍然操作失败求解.....
难道是我漏了什么么?为什么这段代码运行到onclick事件的“sendHttprequest”方法就不再执行了?(textview只显示了“1”和“2”,原本按下按钮触发的sendHTTPrequest方法的“3”没有出现)不明白啊,求大神....代码如下:
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.logging.LogRecord;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button button;
private TextView tx;
public static final int SHOW_RESPONES = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
tx = (TextView) findViewById(R.id.textView);
button.setOnClickListener(this);
tx.setText("1");
}
public void onClick(View view) {
if (view.getId() == R.id.button) {
sendHttpRequest();
tx.setText("2");
}
}
private void sendHttpRequest() {
tx.setText("4");
new Thread(new Runnable() {
@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));
StringBuilder respones = new StringBuilder();
String line;
tx.setText("3");
while ((line = reader.readLine()) != null) {
respones.append(line);
}
Message message = new Message();
message.what = SHOW_RESPONES;
message.obj = respones.toString();
handler.sendMessage(message);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
Log.d("?", "??");
}
}
}
}).start();
}
private Handler handler = new Handler() {
//Why protected
public void handleMessage(Message message) {
switch (message.what) {
case SHOW_RESPONES:
String respones = (String) message.obj;
tx.setText("daole");
tx.setText(respones);
Log.d("haha","hahaha");
}
}
};
}
权限: <uses-permission android:name="android.permission.INTERNET"/>
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.logging.LogRecord;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button button;
private TextView tx;
public static final int SHOW_RESPONES = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
tx = (TextView) findViewById(R.id.textView);
button.setOnClickListener(this);
tx.setText("1");
}
public void onClick(View view) {
if (view.getId() == R.id.button) {
sendHttpRequest();
tx.setText("2");
}
}
private void sendHttpRequest() {
tx.setText("4");
new Thread(new Runnable() {
@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));
StringBuilder respones = new StringBuilder();
String line;
tx.setText("3");
while ((line = reader.readLine()) != null) {
respones.append(line);
}
Message message = new Message();
message.what = SHOW_RESPONES;
message.obj = respones.toString();
handler.sendMessage(message);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
Log.d("?", "??");
}
}
}
}).start();
}
private Handler handler = new Handler() {
//Why protected
public void handleMessage(Message message) {
switch (message.what) {
case SHOW_RESPONES:
String respones = (String) message.obj;
tx.setText("daole");
tx.setText(respones);
Log.d("haha","hahaha");
}
}
};
}
权限: <uses-permission android:name="android.permission.INTERNET"/>
...全文
相关推荐
第一行代码天气示例
第一行代码中最后一个示例程序,采用了百度apistore里面的天气查询接口,地区数据来源于sqllite的数据库文件。
Retrofit网络请求GET请求POST请求
Retrofit网络请求,内含GET、POST请求,POST请求需要用自己的服务器针对性接口测试、GET请求公开接口测试,解析内容简单按需自己项目,很好的框架通俗易懂
SQL Broswer启动请求失败
SQL Broswer启动请求失败错误及解决办法
微信小程序设置全局请求URL及封装wx.request请求操作示例
主要介绍了微信小程序设置全局请求URL及封装wx.request请求操作,结合实例形式分析了微信小程序针对wx.requset请求的封装及调用操作相关实现技巧,需要的朋友可以参考下
微信小程序 网络请求(GET请求)详解
微信小程序 网络请求 GET 微信小程序开发中网络请求必不可少,今天说说最简单的请求.后续会尝试上传下载,Socket这些. 1.一个微信小程序,同时只能有5个网络请求连接。 这个规定应该是微信为了保证用户体验制定的,毕竟是小程序. 2.wx.request(OBJECT) 参数说明: 微信小程序支持GET,POST等请求.用method可以设置. 以下是GET请求的代码: //rate.js //获取应用实例 var app = getApp() Page( { data: { code: 'USD', c


Eclipse
