之前的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.xianlaiwan.cn/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);
? ? ? ?}
? ?}
}
到底是哪里出了問題,求好心人解答,萬分感謝!
2018-02-08
有一種可能,有空指針導致閃退,閃退瞬間刷掉了log所以看不到
2017-04-10
這段代碼沒問題,看看是不是沒加Internet權限