AsyncTask异步交互和httpurlconnection结合使用

时间:2021-11-20 08:37:10

//网络请求数据

package com.baidu.myutils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL; import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo; public class MyUtils { //设置请求方式
public static String Json_url="http://93.gov.cn/93app/get_search.do?key=%E4%B9%9D%E4%B8%89&startNum=0";
//判断网络是否连接
public static boolean Netisavilable(Context context){
//获得网络管理
ConnectivityManager cManager=(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//获得网络详情
NetworkInfo networkInfo=cManager.getActiveNetworkInfo();
if(networkInfo==null||!networkInfo.isAvailable()){
return false;
}
return true; } public static String check_j(String url){
String str="";
StringBuffer sb=new StringBuffer(Json_url);
try {
//创建url
URL myurl=new URL(sb.toString());
HttpURLConnection urlConnection=(HttpURLConnection) myurl.openConnection();
urlConnection.setConnectTimeout();
urlConnection.setReadTimeout();
if(urlConnection.getResponseCode()==){
BufferedReader br=new BufferedReader
(new InputStreamReader
(urlConnection.getInputStream(),"utf-8")); str=br.readLine();
} } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return str;
}
}
package com.baidu.myutils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL; import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo; public class MyUtils { //设置请求方式
public static String Json_url="http://93.gov.cn/93app/get_search.do?key=%E4%B9%9D%E4%B8%89&startNum=0";
//判断网络是否连接
public static boolean Netisavilable(Context context){
//获得网络管理
ConnectivityManager cManager=(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//获得网络详情
NetworkInfo networkInfo=cManager.getActiveNetworkInfo();
if(networkInfo==null||!networkInfo.isAvailable()){
return false;
}
return true; } public static String check_j(String url){
String str="";
StringBuffer sb=new StringBuffer(Json_url);
try {
//创建url
URL myurl=new URL(sb.toString());
HttpURLConnection urlConnection=(HttpURLConnection) myurl.openConnection();
urlConnection.setConnectTimeout();
urlConnection.setReadTimeout();
if(urlConnection.getResponseCode()==){
BufferedReader br=new BufferedReader
(new InputStreamReader
(urlConnection.getInputStream(),"utf-8")); str=br.readLine();
} } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return str;
}
}

//解析数据  AsyncTask异步交互

package combaidu.zong;

import java.util.List;

import com.baidu.adapter.MyAdapter;
import com.baidu.myutils.MyUtils;
import com.baidu.vo.MyData;
import com.baidu.vo.MyNews;
import com.google.gson.Gson; import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.database.CursorJoiner.Result;
import android.view.Menu;
import android.widget.ListView; public class MainActivity extends Activity {
// http://93.gov.cn/93app/get_search.do?key=%E4%B9%9D%E4%B8%89&startNum=0 private ListView lv;
private List<MyData> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
//找到相应的控件
lv = (ListView) findViewById(R.id.lv);
new MyTask().execute(MyUtils.Json_url);
} class MyTask extends AsyncTask<String, String , String>{ @Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String str=params[];
String jstr=MyUtils.check_j(str); return jstr;
}
@Override
protected void onPostExecute(String result) {
System.out.println(result+"--------------"); Gson g=new Gson();
MyNews myNews=g.fromJson(result, MyNews.class);
list = myNews.getList();
System.out.println(list+"-------集合为:-------");
lv.setAdapter(new MyAdapter(MainActivity.this,list));
super.onPostExecute(result);
} } }