課程
/移動開發
/Android
/Android必學-異步加載
老師 能給下視頻的源碼嗎???? [email protected]
2015-06-03
源自:Android必學-異步加載 2-2
正在回答
package com.example.news;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView listView;
private static String URL="http://www.xianlaiwan.cn/api/teacher?type=4&num=30";
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? listView=(ListView) findViewById(R.id.lv_main);
? ? ? ? new NewsAsyncTask().execute(URL);
? ? }
? ??
? ? //將URL對應的JSON格式數據轉化為我么所封裝的NewsBean
? ? private List<NewsBean> getJsonData(String url) {
// TODO Auto-generated method stub
? ? List<NewsBean> newsBeanList=new ArrayList<NewsBean>();
? ? try {
String jsonString=readStream(new java.net.URL(url).openStream());
JSONObject jsonObject;
NewsBean newsBean;
try {
jsonObject = new JSONObject(jsonString);
JSONArray jsonArray = jsonObject.getJSONArray("data");
for(int i=0;i<jsonArray.length();i++)
{
jsonObject = jsonArray.getJSONObject(i);
newsBean =new NewsBean();
newsBean.newsIconUrl = jsonObject.getString("picSmall");
newsBean.newsTitle = jsonObject.getString("name");
newsBean.newsContent = jsonObject.getString("description");
newsBeanList.add(newsBean);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
} catch (IOException e) {
? ? return newsBeanList;
? ? //通過is解析網頁返回的數據
? ? private String readStream(InputStream is)
? ? {
? ? InputStreamReader isr;
? ? String result="";
? ? String line;
isr=new InputStreamReader(is,"utf-8");
BufferedReader br=new BufferedReader(isr);
while((line=br.readLine())!=null)
result+=line;
} catch (UnsupportedEncodingException e) {
return result;
? ?
? ? //實現網絡的異步訪問
? ? class NewsAsyncTask extends AsyncTask<String,Void,List<NewsBean>>
@Override
protected List<NewsBean> doInBackground(String... params) {
return getJsonData(params[0]);
protected void onPostExecute(List<NewsBean> result) {
super.onPostExecute(result);
NewsAdapter adapter = new NewsAdapter(MainActivity.this,result);
listView.setAdapter(adapter);
舉報
了解Android中的異步加載處理方法,這是面試問的最多的知識點
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2016-01-07
package com.example.news;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView listView;
private static String URL="http://www.xianlaiwan.cn/api/teacher?type=4&num=30";
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? listView=(ListView) findViewById(R.id.lv_main);
? ? ? ? new NewsAsyncTask().execute(URL);
? ? }
? ??
? ? //將URL對應的JSON格式數據轉化為我么所封裝的NewsBean
? ? private List<NewsBean> getJsonData(String url) {
// TODO Auto-generated method stub
? ? List<NewsBean> newsBeanList=new ArrayList<NewsBean>();
? ? try {
String jsonString=readStream(new java.net.URL(url).openStream());
JSONObject jsonObject;
NewsBean newsBean;
try {
jsonObject = new JSONObject(jsonString);
JSONArray jsonArray = jsonObject.getJSONArray("data");
for(int i=0;i<jsonArray.length();i++)
{
jsonObject = jsonArray.getJSONObject(i);
newsBean =new NewsBean();
newsBean.newsIconUrl = jsonObject.getString("picSmall");
newsBean.newsTitle = jsonObject.getString("name");
newsBean.newsContent = jsonObject.getString("description");
newsBeanList.add(newsBean);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
? ? return newsBeanList;
}
? ? //通過is解析網頁返回的數據
? ? private String readStream(InputStream is)
? ? {
? ? InputStreamReader isr;
? ? String result="";
? ? try {
? ? String line;
isr=new InputStreamReader(is,"utf-8");
BufferedReader br=new BufferedReader(isr);
while((line=br.readLine())!=null)
{
result+=line;
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
? ?
? ? }
? ? //實現網絡的異步訪問
? ? class NewsAsyncTask extends AsyncTask<String,Void,List<NewsBean>>
? ? {
@Override
protected List<NewsBean> doInBackground(String... params) {
// TODO Auto-generated method stub
return getJsonData(params[0]);
}
@Override
protected void onPostExecute(List<NewsBean> result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
NewsAdapter adapter = new NewsAdapter(MainActivity.this,result);
listView.setAdapter(adapter);
}
? ?
? ? }
? ?
}