package com.example.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.os.AsyncTask;
public class Util extends AsyncTask<String, Void, String>{
private String json;
@Override
protected String doInBackground(String... params) {
try {
URL url=new URL(params[0]);
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
if(connection.getResponseCode()==200){
InputStream is = connection.getInputStream();
byte[] by=new byte[1024];
int len;
ByteArrayOutputStream os=new ByteArrayOutputStream();
while((len=is.read(by))!=-1){
os.write(by, 0, len);
}
json = os.toString("gbk");
return json;
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import com.example.adapter.Adapter;
import com.example.bean.Bean;
import com.example.bean.Bean.Data;
import com.example.util.Util;
import com.google.gson.Gson;
public class SecActivity extends Activity {
private ListView lv;
private List<Data> da;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sec);
lv = (ListView) findViewById(R.id.listView1);
get();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent intent = new Intent(SecActivity.this, ThreActivity.class);
intent.putExtra("source", da.get(arg2).source);
intent.putExtra("dis", da.get(arg2).display_url);
startActivity(intent);
}
});
}
public void get() {
String path = "http://ic.snssdk.com/2/article/v25/stream/?category=news_tech&count=20&bd_latitude=4.9E-324&bd_longitude=4.9E-324&bd_loc_time=1457695555&loc_mode=5&lac=4527&cid=28883&iid=3835029558&device_id=12211880440&ac=wifi&channel=baidu&aid=13&app_name=news_article&version_code=460&device_platform=android&device_type=iToolsAVM&os_api=19&os_version=4.4.4&uuid=352284041361761&openudid=84c1c7b192991cc6";
Util ut = new Util() {
@Override
protected void onPostExecute(String result) {
Gson gson = new Gson();
Bean bean = gson.fromJson(result, Bean.class);
da = bean.data;
lv.setAdapter(new Adapter(da, SecActivity.this));
super.onPostExecute(result);
}
};
ut.execute(path);
}
}