老师,这里报的是执行doInBackground报的错误,但是我找不到错在哪
public class RequestDataAsyncTask extends AsyncTask<Void,Void,String>{
@Override
protected String doInBackground(Void... voids) {
return request("http://www.imooc.com/api/shopping?type=11");
}
private String request(String urlString) {
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(30000);
connection.setRequestMethod("GET");
connection.connect();
int responseCode = connection.getResponseCode();
if(responseCode==HttpURLConnection.HTTP_OK){
InputStreamReader inputStreamReader = new InputStreamReader(connection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
String line;
while((line = bufferedReader.readLine())!=null){
stringBuilder.append(line);
}
bufferedReader.close();
inputStreamReader.close();
return stringBuilder.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//Loading消失,数据处理 刷新界面
FoodResult foodResult = new FoodResult();
try {
JSONObject jsonObject = new JSONObject(result);
final int status = jsonObject.getInt("status");
final String msg = jsonObject.getString("msg");
foodResult.setmStatus(status);
foodResult.setmMessage(msg);
List<FoodInfo> foodInfoList = new ArrayList<>();
JSONArray dataArray = jsonObject.getJSONArray("data");
for (int index = 0; index < dataArray.length(); index++) {
FoodInfo foodInfo = new FoodInfo();
JSONObject tempJSONObject = (JSONObject)dataArray.get(index);
final String name = tempJSONObject.getString("name");
foodInfo.setmName(name);
foodInfoList.add(foodInfo);
}
foodResult.setmLessonInfoList(foodInfoList);
} catch (JSONException e) {
e.printStackTrace();
}
//List<data> item.view
main_list_view.setAdapter(new FoodAdapter(MainActivity.this,foodResult.getmLessonInfoList()));
}
}
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 307 份
- 解答问题 1613 个
本专题是联网及数据处理的必备技能。课程从网络基础知识到线程间协同工作、异步下载处理。介绍了Android内外部文件存储、轻量级数据库SQLite的使用。利用屏幕适配、状态保持、百度地图解决实际问题。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星