編輯:關於Android編程
1、創建要下載數據的實體類
class MyData
{
String imagepath;
String title;
String desc;
public MyData(String imagepath, String title, String desc)
{
super();
this.imagepath = imagepath;
this.title = title;
this.desc = desc;
}
}
2、AsyncTask數據加載及Json解析類
class FileAsyncTask extends AsyncTask
{
@Override
protected String doInBackground(String... params)
{
InputStream is = null;
ByteArrayBuffer byteArrayBuffer = new ByteArrayBuffer(4000);
try
{
URL url = new URL(params[0]);
URLConnection openConnection = url.openConnection();
//設置連接超時的時間
openConnection.setConnectTimeout(3000);
//數據讀取超時的時間
openConnection.setReadTimeout(3000);
is = openConnection.getInputStream();
byte[] buffer = new byte[1024];
int len;
while (-1 != (len = is.read(buffer)))
{
byteArrayBuffer.append(buffer, 0, len);
}
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
runOnUiThread(new Runnable()
{
public void run()
{
//設置加載異常時的控件,通常顯示進度條
}
});
e.printStackTrace();
}
finally
{
if (is != null)
{
try
{
is.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
String res = new String(byteArrayBuffer.toByteArray(), 0,
byteArrayBuffer.length());
return res;
}
// 解析jsonArray數據
@Override
protected void onPostExecute(String result)
{
try
{
JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++)
{
JSONObject jsonObject = jsonArray.getJSONObject(i);
String imagepath = jsonObject.getString("imagepath");
String title = jsonObject.getString("title");
String desc = jsonObject.getString("desc");
MyData myData = new MyData(imagepath, title, desc);
dataList.add(myData);//添加數據
}
myAdapter.notifyDataSetChanged();//通知適配器數據改變
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
super.onPostExecute(result);
}
}
3、調用
private ArrayList dataList = new ArrayList();//聲明全局變量dataList
FileAsyncTask fileAsyncTask = new FileAsyncTask();
fileAsyncTask.execute("http://192.168.56.1:8080/service.txt");
JNI靜態(static)加載OpenCV本文地址: http://blog.csdn.net/caroline_wendy步驟:1. 准備OpenCV-Android庫
你現在還是把錢都存在余額寶?相信你也注意到,現在理財方式越來越多,余額寶的收益也越來越低,如今微信也有理財通,也很多朋友加入購買。對微信理財通手機以了解?下
本android項目是致力於建立一個本地的sqlite數據庫,然後存儲考試的題目,android應用調用sqlite數據庫中的數據,生成相應的答題界面。下面是代碼解析&n
Activity Android中,Activity是所有程序的根本,所有程序的流程都運行在Activity之中,Activity具有自己的生命周期(見http://