HttpClient解析json(涉及AsyncTas异步加载图片)
2016-06-30 15:22
489 查看
====================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();
}
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();
}
相关文章推荐
- 判断是否连接网络以及是否是局域网
- ViewPager无限轮播图+小圆点+HttpURLConnection +handler+Thread
- Uncaught NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'file:///E:/test
- HttpUrlConnection-post请求
- HttpUrConnection_get请求======
- iOS9 http请求换成https请求
- Xml pull解析,XUtils网络请求
- HttpURLConnection用法详解
- 网络理解
- 检查当前网络状态
- http请求带了参数 form序列化 乱码
- NoHttp使用简析——Android网络请求框架(二)
- Http
- AndroidStudio 更新失败,sdk更新失败。http://www.android-studio.org/ 下载AS安装包 跳转到Google,无法下载
- 全局免流原理
- XMLHttpRequest对象
- Linux wget下载https类型文件报错解决方法
- windows 端口转发自带工具 配合n2n用(该工具在本地转发的端口只能在本地访问 ,这一点太不爽了,还是用https://boutell.com/rinetd/方便些)
- 神经网络之激活函数(Activation Function)
- 在MyEclipse中写的工程引入到eclipse下出现错误 发现import javax.servlet.http.HttpSession无法引入