HttpURLConnection—_Post请求
来源:互联网 发布:c语言求最小公约数 编辑:程序博客网 时间:2024/05/14 17:13
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if (msg.what == 0){
DataDataBean dataDataBean = (DataDataBean) msg.obj;
final List<DataDataBean.ResultBean.DataBean> list = dataDataBean.getResult().getData();
//设置适配器
MyAdapter myAdapter = new MyAdapter(MainActivity.this, list);
listView.setAdapter(myAdapter);
/**
* 条目的点击事件
*/
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//跳转.....url路径传过去
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("url",list.get(i).getUrl());
startActivity(intent);
}
});
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list_view);
}
/**
* 点击事件中去获取网络上的json数据
*
* httpUrlConnection请求的post方式
*
* type=top&key=597b4f9dcb50e051fd725a9ec54d6653
*
* get方式的地址:http://v.juhe.cn/toutiao/index?type=top&key=597b4f9dcb50e051fd725a9ec54d6653
*/
public void getDataFromNet(View view){
//访问之前先判断一下网络状态
if (NetWorkUtil.isConn(MainActivity.this)){
new Thread(){
@Override
public void run() {
//获取数据的地址
String path = "http://v.juhe.cn/toutiao/index";
try {
URL url = new URL(path);
//打开连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//设置
connection.setRequestMethod("POST");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
//设置请求内容的类型
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
//设置可以向外输出...因为post方式的时候参数是以流的形式写给服务器,所以设置向外输出
connection.setDoOutput(true);
//参数
String params = "type=top&key=597b4f9dcb50e051fd725a9ec54d6653";
//把参数以流的形式写给服务器
connection.getOutputStream().write(params.getBytes());//代码到这个位置,,,参数写给服务器
//获取
int responseCode = connection.getResponseCode();
if (responseCode == 200){
//获取到服务器返回的输入字节流....装的是json格式的数据
InputStream inputStream = connection.getInputStream();
//把字节流转成字符串
String json = streamToString(inputStream,"utf-8");
//解析获取到的json格式的字符串
Gson gson = new Gson();
//生成变量...alt+enter.....此时所有的数据都装到bean里面了
DataDataBean dataDataBean = gson.fromJson(json, DataDataBean.class);
//由于处在子线程,不能设置适配器,,,所以把bean发送到主线程
Message message = Message.obtain();
message.what = 0;
message.obj = dataDataBean;
handler.sendMessage(message);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}else {
//没有网络的时候弹窗提示
NetWorkUtil.showNoNetWorkDlg(MainActivity.this);
}
}
/**
* 把字节流转换为字符串
* @param inputStream
* @param
* @return
*/
private String streamToString(InputStream inputStream, String charset) {
//1.将输入的字节流转为字符流
try {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream,charset);
//2.快速的读取需要缓冲流
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String s = null;
StringBuilder builder = new StringBuilder();//字符串缓冲区
while ((s = bufferedReader.readLine()) != null){
//添加到字符串缓冲区
builder.append(s);
}
bufferedReader.close();
//返回字符串
return builder.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
public void handleMessage(Message msg) {
if (msg.what == 0){
DataDataBean dataDataBean = (DataDataBean) msg.obj;
final List<DataDataBean.ResultBean.DataBean> list = dataDataBean.getResult().getData();
//设置适配器
MyAdapter myAdapter = new MyAdapter(MainActivity.this, list);
listView.setAdapter(myAdapter);
/**
* 条目的点击事件
*/
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//跳转.....url路径传过去
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("url",list.get(i).getUrl());
startActivity(intent);
}
});
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list_view);
}
/**
* 点击事件中去获取网络上的json数据
*
* httpUrlConnection请求的post方式
*
* type=top&key=597b4f9dcb50e051fd725a9ec54d6653
*
* get方式的地址:http://v.juhe.cn/toutiao/index?type=top&key=597b4f9dcb50e051fd725a9ec54d6653
*/
public void getDataFromNet(View view){
//访问之前先判断一下网络状态
if (NetWorkUtil.isConn(MainActivity.this)){
new Thread(){
@Override
public void run() {
//获取数据的地址
String path = "http://v.juhe.cn/toutiao/index";
try {
URL url = new URL(path);
//打开连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//设置
connection.setRequestMethod("POST");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
//设置请求内容的类型
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
//设置可以向外输出...因为post方式的时候参数是以流的形式写给服务器,所以设置向外输出
connection.setDoOutput(true);
//参数
String params = "type=top&key=597b4f9dcb50e051fd725a9ec54d6653";
//把参数以流的形式写给服务器
connection.getOutputStream().write(params.getBytes());//代码到这个位置,,,参数写给服务器
//获取
int responseCode = connection.getResponseCode();
if (responseCode == 200){
//获取到服务器返回的输入字节流....装的是json格式的数据
InputStream inputStream = connection.getInputStream();
//把字节流转成字符串
String json = streamToString(inputStream,"utf-8");
//解析获取到的json格式的字符串
Gson gson = new Gson();
//生成变量...alt+enter.....此时所有的数据都装到bean里面了
DataDataBean dataDataBean = gson.fromJson(json, DataDataBean.class);
//由于处在子线程,不能设置适配器,,,所以把bean发送到主线程
Message message = Message.obtain();
message.what = 0;
message.obj = dataDataBean;
handler.sendMessage(message);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}else {
//没有网络的时候弹窗提示
NetWorkUtil.showNoNetWorkDlg(MainActivity.this);
}
}
/**
* 把字节流转换为字符串
* @param inputStream
* @param
* @return
*/
private String streamToString(InputStream inputStream, String charset) {
//1.将输入的字节流转为字符流
try {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream,charset);
//2.快速的读取需要缓冲流
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String s = null;
StringBuilder builder = new StringBuilder();//字符串缓冲区
while ((s = bufferedReader.readLine()) != null){
//添加到字符串缓冲区
builder.append(s);
}
bufferedReader.close();
//返回字符串
return builder.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
阅读全文
0 0
- HttpURLConnection—_Post请求
- Android HttpUrlConnection 发送Post请求 PHP页面$_POST接收不到问题
- 请求数据—HttpUrlConnection
- HttpURLConnection请求
- HttpURLConnection请求
- HttpURLConnection请求
- HttpURLConnection 请求
- HttpUrlConnection请求
- android网络技术之——HttpURLConnection请求
- Android中网络请求—HttpClient、HttpURLConnection
- $_POST
- HttpURLConnection请求服务
- HttpURLConnection post 请求
- HttpURLConnection请求图片
- HttpURLConnection post请求
- 请求网络之HttpUrlConnection
- HttpURLConnection数据请求
- HttpUrlconnection get请求数据
- 高级java工程师面试必备:jvm知识体系总揽
- 高通kernel顶层目录下的AndroidKernel.mk文件
- crontab格式
- jQuery手机长按触发事件
- @RequestMapping注解的参数
- HttpURLConnection—_Post请求
- C# PowerPoint操作的基本用法。
- mysql、oracle、sqlserver查询对应数据库的表名称和别名
- go语言环境搭建与开发环境配置
- 进程的管理
- loadrunner安装过程中的坑
- Vue中数组数据驱动class的bug解决办法
- Uri的相互转换
- npm安装时出现错误解决办法及mac下升级node.js