httpclient
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了httpclient相关的知识,希望对你有一定的参考价值。
package com.example.twoeaxm;
import java.io.IOException;
import java.util.List;
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.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import com.bw.adapter.MyAdapter;
import com.bw.bean.All;
import com.bw.bean.Content;
import com.bw.bean.Data;
import com.google.gson.Gson;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(){
public void run(){
getJson();
}
}.start();
}
private void getJson() {
String url="http://api.sina.cn/sinago/list.json?channel=hdpic_story&adid=4ad30dabe134695c3d7c3a65977d7e72&from=6042095012&chwm=12050_0001&imei=867064013906290&uid=802909da86d9f5fc&p=1";
HttpClient httpClient=new DefaultHttpClient();
HttpPost httppost=new HttpPost(url);
try {
HttpResponse execute = httpClient.execute(httppost);
StatusLine statusLine = execute.getStatusLine();
int statusCode = statusLine.getStatusCode();
HttpEntity entity = execute.getEntity();
String string = EntityUtils.toString(entity);
Gson gson=new Gson();
Data all=gson.fromJson(string, Data.class);
final List<Content> content=all.getData().getList();
// for (int i = 0; i < content.size(); i++) {
// Log.i("TAG", content.get(i).toString());
// }
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
ListView lv=(ListView) findViewById(R.id.listview);
lv.setAdapter(new MyAdapter(MainActivity.this,content));
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id) {
// TODO Auto-generated method stub
Intent it=new Intent(MainActivity.this,ItemActivity.class);
it.putExtra("list", content.get(position).getLink());
startActivity(it);
Log.i("TAG", "?????");
}
});
}
});
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
以上是关于httpclient的主要内容,如果未能解决你的问题,请参考以下文章