package com.example.news;
import java.io.BufferedReader;
import java.net.URL;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
public class MainActivity extends Activity {
private static String URL = "http://www.imooc.com/api/teacher?type=4&num=30";
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.mainListView);
new NewAsyncTask().execute(URL);
}
private List<NewsBean> getJsonDate(String url)
{
List<NewsBean> newBeanList = new ArrayList<NewsBean>();
try {
String jsonString = readStream(new URL(url).openStream());
Log.d("xyz", jsonString);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return newBeanList;
}
private String readStream(InputStream is){
InputStreamReader isr;
String result = "";
try {
String line = "";
isr = new InputStreamReader(is,"utf-8");
BufferedReader br = new BufferedReader(isr);
while ((line = br.readLine())!=null);
{
result += line;
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
class NewAsyncTask extends AsyncTask<String, Void, List<NewsBean>>{
@Override
protected List<NewsBean> doInBackground(String... arg0) {
// TODO Auto-generated method stub
return getJsonDate(arg0[0]);
}
}
}
qq_冲锋_1
justindo
相关分类