1.简单介绍一下HttpClient的get方法请求数据
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
/**
* 权限 intenet 线程
*
* @author Administrator
*
*/
public class MainActivity extends Activity {
private String tag = "MainActivity-->";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.main_list_view);
bar = (ProgressBar) findViewById(R.id.progress);
// 从网络请求数据
getDataFromNet();
}
// 从网络请求数据
private void getDataFromNet() {
//
bar.setVisibility(View.VISIBLE);
// 开启子线程
new Thread() {
public void run() {
/**
* HttpClient 1.get,拼接字符串 2.post,通过流进行数据的传输
*
*/
// get
// 1.创建HttpClient对象
HttpClient client = new DefaultHttpClient();
// 2.创建请求方法
HttpGet get = new HttpGet(Url.TOP);
// 3.执行get请求
try {
HttpResponse response = client.execute(get);
// 取出数据
StatusLine status = response.getStatusLine();// 状态行
int code = status.getStatusCode();// 状态码
if (code == 200) {
// 取出新闻内容
HttpEntity entity = response.getEntity();// 实体
InputStream content = entity.getContent();// 内容
// 将流--->String
String json = inputStream2String(content);
Log.d(tag, "返回结果--》" + json);
// 解析数据
ArrayList
list = parseJson(json);
// 发送到主线程(Handler)
Message message = mHandler.obtainMessage();
message.obj = list;
mHandler.sendMessage(message);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();
}
// handler
Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
Log.d(tag, "收到消息");
// 隐藏进度条
bar.setVisibility(View.GONE);
// 接收数据并展示
ArrayList
list = (ArrayList
) msg.obj;
Log.e(tag, "list--->" + list);
listView.setAdapter(new ArrayAdapter
(MainActivity.this,
android.R.layout.simple_list_item_1, list));
};
};
private ListView listView;
private ProgressBar bar;
public String inputStream2String(InputStream content) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
int len = 0;
byte[] buffer = new byte[1024];
try {
while ((len = content.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return os.toString();
}
// 解析json数据
public ArrayList
parseJson(String json) { ArrayList
list = new ArrayList
(); try { JSONObject jsonObject = new JSONObject(json); JSONObject jsonObject2 = jsonObject.getJSONObject("result"); JSONArray jsonArray = jsonObject2.getJSONArray("data"); for (int i = 0; i < jsonArray.length(); i++) { list.add(jsonArray.getJSONObject(i).getString("title")); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } }