Shoucang1
package com.baidu.text1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import com.baidu.adapter.MyAdapter;
import com.bwie.vo.List;
import com.bwie.vo.Super;
import com.google.gson.Gson;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity {
public static final 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";
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
String str = (String) msg.obj;
perseJson(str);
};
};
private ListView listView;
private ArrayList<List> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
listView = (ListView) findViewById(R.id.lv);
new Thread() {
public void run() {
String str = getJsonFromServer();
handler.sendMessage(handler.obtainMessage(1, str));
};
}.start();
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(MainActivity.this, NewActivity.class);
intent.putExtra("list", list.get(position));
startActivity(intent);
}
});
}
private void perseJson(String str) {
// TODO Auto-generated method stub
Gson g = new Gson();
Super superNews = g.fromJson(str, Super.class);
list = superNews.getData().getList();
listView.setAdapter(new MyAdapter(MainActivity.this, list));
System.out.println(superNews.getData().getList().toString());
}
private String getJsonFromServer() {
StringBuffer sb = new StringBuffer();
try {
URL url = new URL(URL);
HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();
urlcon.setConnectTimeout(5000);
urlcon.setReadTimeout(5000);
if (urlcon.getResponseCode() == 200) {
BufferedReader br = new BufferedReader(new InputStreamReader(
urlcon.getInputStream(), "utf-8"));
String str = "";
while ((str = br.readLine()) != null) {
sb.append(str);
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sb.toString();
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}