之前的logcat都能检测到,但是现实空白,现在程序闪退
代码如下
import android.app.Activity;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOError;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
private ListView mListView;
private static String URL="http://www.imooc.com/api/teacher?type=4&num=30";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView= (ListView) findViewById(R.id.lv_main);
new NewsAsyncTask().execute(URL);
}
private List<NewsBean>getJsonData(String url){
List<NewsBean>newsBeanList=new ArrayList<>();
try {
String jsonString = readStream(new URL(url).openStream());
JSONObject jsonObject;
NewsBean newsBean;
try {
jsonObject=new JSONObject(jsonString);
JSONArray jsonArray=jsonObject.getJSONArray("data");
for (int i=0;i<jsonArray.length();i++){
jsonObject=jsonArray.getJSONObject(i);
newsBean=new NewsBean();
newsBean.newsIconUrl=jsonObject.getString("picSmall");
newsBean.newsTitle=jsonObject.getString("name");
newsBean.newsContent=jsonObject.getString("description");
newsBeanList.add(newsBean);
}
} catch (JSONException e) {
e.printStackTrace();
}
}catch (IOException e){
e.printStackTrace();
}
return newsBeanList;
}
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){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
class NewsAsyncTask extends AsyncTask<String,Void,List<NewsBean>>{
@Override
protected List<NewsBean> doInBackground(String... params) {
return getJsonData(params[0]);
}
@Override
protected void onPostExecute(List<NewsBean> newsBeans) {
super.onPostExecute(newsBeans);
NewsAdapter adapter=new NewsAdapter(MainActivity.this,newsBeans);
mListView.setAdapter(adapter);
}
}
}
到底是哪里出了问题,求好心人解答,万分感谢!