我有一个有趣的问题给你。我在Android中有一个函数,它通过从远程服务器加载列表视图来与UI进行交互。正如预期的那样,这使得我的应用程序非常缓慢。在android中创建一个函数异步/线程
随着代码的最低金额,我怎么把这个功能在自己的线程或同步任务,同时仍然允许它像一个正常的功能
它没有非常有效的执行。我只需要用尽可能少的代码就可以做到这一点。
try
{ //getjson file
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
HttpResponse responseGet = client.execute(get);
HttpEntity resEntityGet = responseGet.getEntity();
if (resEntityGet != null) {
InputStream instream = resEntityGet.getContent();
BufferedReader str = new BufferedReader(new InputStreamReader(
instream));
String ans = new String("");
build = new String("");
while ((ans = str.readLine()) != null) {
build = build + ans;
}
//parse json file
JSONObject total = new JSONObject(build);
JSONArray prices = null;
if (!total.isNull("title")){
Prices = title.getJSONArray("title"));}
ArrayList<data> newArray = new ArrayList<data>();
for(int i=0;i<Prices.length();i++)
{
JSONObject price = null;
price = Prices.getJSONObject(i);
data name = new data (price.getString("StoreName"),price.getString("price"),price.getString("Link"));
newArray.add(name);
}
stockArr = new data[newArray.size()];
stockArr = newArray.toArray(stockArr);
//add to listview
ListView listView = (ListView) findViewById(R.id.mylist);
listadapter adapter = new listadapter(this,R.layout.listview_item_row, stockArr);
listView.setAdapter(adapter);
}
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(),e.toString(), 10).show();
}
“随着代码的最低金额” - 所以是不玩'代码Golf',IMNSHO – lenik
发布您的代码的地方。理想情况下,您应该访问远程服务器,获取数据,在后台解析数据并更新onPostExecute中的适配器。另外,如果你想要中间更新,你可以使用onProgressUpdate,当分析巨大的响应时很有用 – Atrix1987
你刚说的哪些东西是可跳过的? – gaara87