尝试用Gson解析,代码报错
public class MainActivity extends AppCompatActivity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView= (ListView) findViewById(R.id.main_list_view);
new QuestDateAsyncTask().execute();
}
public class QuestDateAsyncTask extends AsyncTask<Void,Void,String>{
@Override
protected String doInBackground(Void... params) {
return request("http://www.imooc.com/api/teacher?type=2");
}
private String request(String urlString) {
try {
URL url=new URL(urlString);
HttpURLConnection coon= (HttpURLConnection) url.openConnection();
coon.setRequestMethod("GET");
coon.setConnectTimeout(6000);
if(coon.getResponseCode()==200){
InputStreamReader inputStreamReader=new InputStreamReader(coon.getInputStream());
BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
StringBuilder stringBuilder=new StringBuilder();
String line;
while ((line= bufferedReader.readLine())!=null){
stringBuilder.append(line);
}
inputStreamReader.close();
bufferedReader.close();
return stringBuilder.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Result result=new Result();
try {
JSONObject jsonObject=new JSONObject(s);
String date=jsonObject.getString("date");
Gson gson=new Gson();
ArrayList<Outline> outlines=gson.fromJson(date,new TypeToken<ArrayList<Outline>>(){}.getType());
result.setOutlines(outlines);
} catch (JSONException e) {
e.printStackTrace();
}
/* ArrayList<Outline> outlines=new ArrayList();
outlines.add(new Outline("asdasdasdasdasdasdasd"));
outlines.add(new Outline("asdasdasdasdasdasdasdasdasdasd"));
outlines.add(new Outline("asdasdasdaasdasdsdasdasdasd"));*/
用这几行注释的代码测了一下,Adapter工作正常
listView.setAdapter(new RequestDateAdapter(MainActivity.this,result.getOutlines()));
}
}
}
========================================================
报错:
09-29 06:07:31.055 3434-3434/com.example.administrator.listviewdemo E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from GradienCache
09-29 06:07:31.055 3434-3434/com.example.administrator.listviewdemo E/OpenGLRenderer: MAX_TEXTURE_SIZE: 16384
09-29 06:07:31.071 3434-3434/com.example.administrator.listviewdemo E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
09-29 06:07:31.071 3434-3434/com.example.administrator.listviewdemo E/OpenGLRenderer: MAX_TEXTURE_SIZE: 16384
正在回答
com.example.administrator.listviewdemo这是哪个项目?不是本module的吧?如果不是直接运行此MainActivity ;如果是你clean一下。祝:学习愉快
- 参与学习 人
- 提交作业 307 份
- 解答问题 1613 个
本专题是联网及数据处理的必备技能。课程从网络基础知识到线程间协同工作、异步下载处理。介绍了Android内外部文件存储、轻量级数据库SQLite的使用。利用屏幕适配、状态保持、百度地图解决实际问题。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星