Listview 多条目展示_AsyncTask
来源:互联网 发布:网络信息安全 电子书 编辑:程序博客网 时间:2024/06/07 06:00
package com.example.day0906;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import com.google.gson.Gson;
import android.R.integer;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ListView;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ListView;
public class MyAsyncTask extends AsyncTask<String, integer, String> {
static DataBean mDatabean;
Context con;
String url;
String type;
ListView listView;
public MyAsyncTask(Context con, String url, String type, ListView listView) {
super();
this.url=url;
this.con=con;
this.type=type;
this.listView=listView;
static DataBean mDatabean;
Context con;
String url;
String type;
ListView listView;
public MyAsyncTask(Context con, String url, String type, ListView listView) {
super();
this.url=url;
this.con=con;
this.type=type;
this.listView=listView;
}
//网络请求
public void GetDatas(String murl, String mtype) {
//头条服务器地址
try {
URL url = new URL(murl);
//打开连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//设置
connection.setRequestMethod(mtype);
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
public void GetDatas(String murl, String mtype) {
//头条服务器地址
try {
URL url = new URL(murl);
//打开连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//设置
connection.setRequestMethod(mtype);
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
//post方式要提交参数给服务器...以流的方式提交给服务器
connection.setDoOutput(true);
//设置请求参数的类型
//connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setDoOutput(true);
//设置请求参数的类型
//connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
String params = "key=8d6e3228d25298f13af4fc40ce6c9679&num=10";
//参数以流的形式写给服务器...字节数组的形式写出去
connection.getOutputStream().write(params.getBytes());
//参数以流的形式写给服务器...字节数组的形式写出去
connection.getOutputStream().write(params.getBytes());
//响应
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
//获取输入字节流
InputStream inputStream = connection.getInputStream();
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
//获取输入字节流
InputStream inputStream = connection.getInputStream();
String json = streamToString(inputStream, "utf-8");
Log.i("json", json);
//解析...解析完成之后发送到主线程,设置适配器
Gson gson = new Gson();
mDatabean = gson.fromJson(json, DataBean.class);
Log.i("json", mDatabean.getCode());
//解析...解析完成之后发送到主线程,设置适配器
Gson gson = new Gson();
mDatabean = gson.fromJson(json, DataBean.class);
Log.i("json", mDatabean.getCode());
}
} catch (Exception e) {
e.printStackTrace();
}
}
private String streamToString(InputStream inputStream, String charset) {
try {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, charset);
try {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, charset);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String s = null;
StringBuilder builder = new StringBuilder();
while ((s = bufferedReader.readLine()) != null) {
builder.append(s);
}
String s = null;
StringBuilder builder = new StringBuilder();
while ((s = bufferedReader.readLine()) != null) {
builder.append(s);
}
bufferedReader.close();
return builder.toString();
return builder.toString();
} catch (Exception e) {
e.printStackTrace();
}
e.printStackTrace();
}
return null;
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
List<DataBean.NewslistBean> listbean=mDatabean.getNewslist();
//设置适配器
MyAdapter myAdapter = new MyAdapter(listbean,con);
listView.setAdapter(myAdapter);
protected void onPostExecute(String s) {
super.onPostExecute(s);
List<DataBean.NewslistBean> listbean=mDatabean.getNewslist();
//设置适配器
MyAdapter myAdapter = new MyAdapter(listbean,con);
listView.setAdapter(myAdapter);
}
public MyAsyncTask() {
super();
}
super();
}
@Override
protected String doInBackground(String... strings) {
GetDatas(url, type);
return null;
}
protected String doInBackground(String... strings) {
GetDatas(url, type);
return null;
}
}
阅读全文
0 0
- Listview 多条目展示_AsyncTask
- ListView多条目展示
- ListView多条目展示
- listview多条目展示
- listView 多条目展示
- ListView多条目展示
- listview多条目展示
- ListView多条目展示
- Listview(多条目展示)
- listview多条目展示
- ListView 多条目展示
- listview(多条目展示)
- ListView多条目展示
- ListView 多条目展示
- ListView多条目展示
- ListView的多条目展示
- listview的多条目展示
- ListView多条目的展示
- Redis 在Linux下的基本安装和使用
- Listview 多条目展示_适配器
- jQueryday04(下拉菜单完全 dom对象和jQuery对象相互转换)
- Linux系统ELF程序的执行过程
- ubuntu16.04安装matlab glnx64(2016b/2017a)完全方案
- Listview 多条目展示_AsyncTask
- 少见但非常有用的MongoDB命令
- Listview 多条目展示_Application
- uva 11149 矩阵快速幂 求矩阵1-n次幂的和
- 关于通过Ajax解决PHP与JS数据交互问题
- C语言实现简单的注册、登录、退出
- Python技巧
- Listview 多条目展示_MainActivity
- 开源 java CMS