HttpClient解析json(涉及AsyncTas异步加载图片)

====================utils下的NetWorkUtils==================

package com.example.utils;



import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;


import android.util.Log;


public class NetWorkUtils {


static String tag = "TAG";


public static String getStr(String htt) {
String path = htt + "18938f9a9808b846aff0801f51d6efde";

HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(path);
try {
HttpResponse response = client.execute(get);
int code = response.getStatusLine().getStatusCode();

if (code ==200) {
String result = EntityUtils.toString(response.getEntity());

/*ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = response.getEntity().getContent();

byte[] arr = new byte[1024];
int len = 0;

while ((len = is.read(arr))!=-1) {
baos.write(arr, 0, len,"utf-8");
}*/


return result;

}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

}



=====================在mainActivity中调用======================

String htt = "http://japi.juhe.cn/comic/book?key=";


/*
* 解析
*/
private void initData() {
new AsyncTask<Void, Void, String>() {




@Override
protected String doInBackground(Void... params) {


return NetWorkUtils.getStr(htt);
}

/**

*相当于子线程

/
@Override
protected void onPostExecute(String result) {


Gson gson = new Gson();
Type type = new TypeToken<BookList>() {
}.getType();


blist = gson.fromJson(result, type);
bl = blist.result.bookList;


// Log.e("TAG", bl.toString());
// 添加适配器,
lv.setAdapter(new MyBaseAdapter(getActivity(),bl));
}
}.execute();


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值