課程
/移動開發
/Android
/Android必學-異步加載
請問如何出現老師的界面??!
2016-01-09
源自:Android必學-異步加載 2-4
正在回答
可以用Log.e(“aa--ff”,傳的值);
qq_彩_0 提問者
<html> <html>
到底怎么解決的,重新安裝軟件嗎?求指教
水淼1
問題解決了,軟件的問題,代碼正確!
出現這樣的情況 RESTART重復相同的問題啊
打上斷點后出現后。輸入Log.e("aa--ff","getJsonData");?????端口又出現問題了,請問怎么解決,謝謝
最主要的這一步怎么整體解決啊,謝謝?。。。?!
package com.yc.asynctask;import android.app.Activity;import android.os.AsyncTask;import android.os.Bundle;import android.util.Log;import android.widget.ListView;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.net.URL;import java.util.ArrayList;import java.util.List;public class MainActivity extends Activity { ? ?private ListView mListView; ? ?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); ? ? ? ?mListView = (ListView) findViewById(R.id.lv_main); ? ? ? ?new NewsAsyncTask().execute(URL); ? ?} ? ?private List<NewsBean> getJsonData(String url) { ? ? ? ?List<NewsBean> newsBeanList = new ArrayList<>(); ? ? ? ?try { ? ? ? ? ? ?String jsonString = readStream(new 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(); ? ? ? ? ? ? ? ? ? ?//從jsonObject中得到每個元素數據給newsBean ? ? ? ? ? ? ? ? ? ?newsBean.newsIconUrl = jsonObject.getString("picSmall"); ? ? ? ? ? ? ? ? ? ?newsBean.newsTitle = jsonObject.getString("name"); ? ? ? ? ? ? ? ? ? ?newsBean.newsContent = jsonObject.getString("description"); ? ? ? ? ? ? ? ? ? ?newsBeanList.add(newsBean); ? ? ? ? ? ? ? ?} ? ? ? ? ? ?} catch (JSONException e) { ? ? ? ? ? ? ? ?e.printStackTrace(); ? ? ? ? ? ?} ? ? ? ?} catch (IOException e) { ? ? ? ? ? ?e.printStackTrace(); ? ? ? ?} ? ? ? ?Log.e("aa--ff","getJsonData"); ? ? ? ?return newsBeanList; ? ?} ? ?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) { ? ? ? ? ? ?e.printStackTrace(); ? ? ? ?} catch (IOException e) { ? ? ? ? ? ?e.printStackTrace(); ? ? ? ?} ? ? ? ?return result; ? ?} ? ?class NewsAsyncTask extends AsyncTask<String, Void, List<NewsBean>>{ ? ? ? ?@Override ? ? ? ?protected List<NewsBean> doInBackground(String... params) { ? ? ? ? ? ?return getJsonData(params[0]); ? ? ? ?} ? ?}}
舉報
了解Android中的異步加載處理方法,這是面試問的最多的知識點
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2016-01-09
可以用Log.e(“aa--ff”,傳的值);
2016-03-26
2016-03-12
到底怎么解決的,重新安裝軟件嗎?求指教
2016-01-11
問題解決了,軟件的問題,代碼正確!
2016-01-10
出現這樣的情況 RESTART重復相同的問題啊
2016-01-10
打上斷點后出現后。輸入Log.e("aa--ff","getJsonData");?????端口又出現問題了,請問怎么解決,謝謝
最主要的這一步怎么整體解決啊,謝謝?。。。?!
package com.yc.asynctask;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
? ?private ListView mListView;
? ?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);
? ? ? ?mListView = (ListView) findViewById(R.id.lv_main);
? ? ? ?new NewsAsyncTask().execute(URL);
? ?}
? ?private List<NewsBean> getJsonData(String url) {
? ? ? ?List<NewsBean> newsBeanList = new ArrayList<>();
? ? ? ?try {
? ? ? ? ? ?String jsonString = readStream(new 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();
? ? ? ? ? ? ? ? ? ?//從jsonObject中得到每個元素數據給newsBean
? ? ? ? ? ? ? ? ? ?newsBean.newsIconUrl = jsonObject.getString("picSmall");
? ? ? ? ? ? ? ? ? ?newsBean.newsTitle = jsonObject.getString("name");
? ? ? ? ? ? ? ? ? ?newsBean.newsContent = jsonObject.getString("description");
? ? ? ? ? ? ? ? ? ?newsBeanList.add(newsBean);
? ? ? ? ? ? ? ?}
? ? ? ? ? ?} catch (JSONException e) {
? ? ? ? ? ? ? ?e.printStackTrace();
? ? ? ? ? ?}
? ? ? ?} catch (IOException e) {
? ? ? ? ? ?e.printStackTrace();
? ? ? ?}
? ? ? ?Log.e("aa--ff","getJsonData");
? ? ? ?return newsBeanList;
? ?}
? ?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) {
? ? ? ? ? ?e.printStackTrace();
? ? ? ?} catch (IOException e) {
? ? ? ? ? ?e.printStackTrace();
? ? ? ?}
? ? ? ?return result;
? ?}
? ?class NewsAsyncTask extends AsyncTask<String, Void, List<NewsBean>>{
? ? ? ?@Override
? ? ? ?protected List<NewsBean> doInBackground(String... params) {
? ? ? ? ? ?return getJsonData(params[0]);
? ? ? ?}
? ?}
}