所以这个问题困扰了我一段时间,因为我似乎无法找到一种简单、方便和自然的方式来更新 ui,同时从外部位置(Web 服务器)获取数据。
我将勾勒出我的场景:
我有一个网络服务器,它为 JSONArray 和 JSONObject 提供服务。它总是返回这个,因为我不喜欢空指针。我试图编写一个扩展类AsyncTask
。我成功地做到了,它看起来像这样:
public class Server extends AsyncTask<String, Integer, Object>{
private String fetch_url;
private String return_type;
private Activity ct;
public Server() {
}
public Server(Activity ct) {
this.ct = ct;
}
@Override
protected void onPreExecute()
{
}
@Override
protected Object doInBackground(String... url) {
this.fetch_url = url[0];
this.return_type = url[1];
if(return_type.equals("json_object")) {
return urlToJsonObject(this.fetch_url);
} else {
return urlToJsonArray(this.fetch_url);
}
}
private String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
protected JSONObject urlToJsonObject(String url) {
JSONObject arr = null;
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet(url);
// Execute the request
HttpResponse response;
try {
response = httpclient.execute(httpget);
// Get hold of the response entity
HttpEntity entity = response.getEntity();
if (entity != null) {
// A Simple JSON Response Read
InputStream instream = entity.getContent();
String result= convertStreamToString(instream);
// A Simple JSONArray Creation
arr = new JSONObject(result);
// Closing the input stream will trigger connection release
instream.close();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return arr;
}
protected JSONArray urlToJsonArray(String url) {
JSONArray arr = null;
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet(url);
// Execute the request
HttpResponse response;
try {
response = httpclient.execute(httpget);
// Get hold of the response entity
HttpEntity entity = response.getEntity();
if (entity != null) {
// A Simple JSON Response Read
InputStream instream = entity.getContent();
String result= convertStreamToString(instream);
// A Simple JSONArray Creation
arr = new JSONArray(result);
// Closing the input stream will trigger connection release
instream.close();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return arr;
}
}
数据输入后,我用 getter 和 setter 将其转换为我自己的模型。然后我想用这些结果更新我的用户界面,但由于某种原因,我总是收到空指针。
例如 :
- 我有一个 GridLayout,我想用多个 PhotoAlbums 填充。
- 首先,我从我的网络服务器获取这些记录。
- 然后处理每张专辑。
- 然后,如果将单个专辑添加到 GridLayout 中,则需要更新。并且在阻止 ui 时不是一次全部。
从外部来源获取数据并在加载时更新 ui 的一种很好的中央方式是什么?有人可以指出我的技术、教程、方法、见解等。我对此非常好奇。