为了账号安全,请及时绑定邮箱和手机立即绑定

求助,一运行程序就崩溃。。底下是源代码,大家帮帮忙。。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package com.tired.itemlist;
 
import java.io.BufferedReader;
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 org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
 
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 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<NewsBean>();  
            try {
                String  jsonString = readStream(new java.net.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) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return newsBeanList;      
    }
 
    private String readStream(InputStream is) {
        InputStreamReader isr;
        String result2 = "";
        try {
            String line = "";
            isr = new InputStreamReader(is, "utf-8");
            BufferedReader br = new BufferedReader(isr);
            try {
                while ((line = br.readLine()) != null) {
                    result2 += line;
                    Log.d("main", result2);
                }
            catch (IOException e) {
                e.printStackTrace();
            }
        catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result2;
    }
 
    class NewsAsyncTask extends AsyncTask<String, Void, List<NewsBean>> {
        @Override
        protected List<NewsBean> doInBackground(String... arg0) {
            return getJsonData(arg0[0]);
        }
 
        @Override
        protected void onPostExecute(List<NewsBean> result) {
            super.onPostExecute(result);
            NewsAdapter adapter = new NewsAdapter(MainActivity.this, result);
            mlistView.setAdapter(adapter);
        }
    }
 
}
 
 
 
 
 
 
 
package com.tired.itemlist;
 
import java.util.List;
 
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
 
public class NewsAdapter extends BaseAdapter {
 
    private List<NewsBean> mList;
    private LayoutInflater mInflater;
     
    public NewsAdapter(Context context,List<NewsBean> data){
        mInflater = LayoutInflater.from(context);
        mList =data;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mList.size();
    }
 
    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return mList.get(arg0);
    }
 
    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }
 
    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub
        ViewHolder viewHolder;
        if(arg1 ==null){
            viewHolder =new ViewHolder();
            arg1 =mInflater.inflate(R.layout.item_layout,null);
            viewHolder.ivIcon =(ImageView) arg1.findViewById(R.id.icon);
            viewHolder.tvTitle =(TextView) arg1.findViewById(R.id.tv_title);
            viewHolder.tvContent = (TextView) arg1.findViewById(R.id.tv_content);
            arg1.setTag(viewHolder);
        }else{
            viewHolder =(ViewHolder) arg1.getTag();
        }
        viewHolder.ivIcon.setImageResource(R.drawable.ic_launcher);
        viewHolder.tvTitle.setText(mList.get(arg0).newsTitle);
        viewHolder.tvContent.setText(mList.get(arg0).newsContent);
        return arg1;
    }
     
    class ViewHolder{
        public TextView tvTitle,tvContent;
        public ImageView ivIcon;
    }
 
}
 
 
 
 
 
package com.tired.itemlist;
 
public class NewsBean {
    public String newsIconUrl;
    public String newsTitle;
    public String newsContent;
}


正在回答

代码语言

1 回答

AndroidManifest里面是不是没有添加<uses-permission android:name="android.permission.INTERNET"/>。

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Android必学-异步加载
  • 参与学习       50625    人
  • 解答问题       311    个

了解Android中的异步加载处理方法,这是面试问的最多的知识点

进入课程

求助,一运行程序就崩溃。。底下是源代码,大家帮帮忙。。

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号