栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

从AsyncTask类返回数据

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从AsyncTask类返回数据

对我来说,关键是创建一个名为URLWithParams的类或其他类,因为AsyncTask将只允许发送一种类型的输入,并且我需要URL和HTTP请求的参数。

public class URLWithParams {    public String url;    public List<NamevaluePair> namevaluePairs;    public URLWithParams()    {        namevaluePairs = new ArrayList<NamevaluePair>();    }}

然后将其发送到JSONClient:

public class JSonClient extends AsyncTask<URLWithParams, Void, String> {    private final static String TAG = "JSONClient";    ProgressDialog progressDialog ;    GetJSonListener getJSONListener;    public JSonClient(GetJSonListener listener){        this.getJSonListener = listener;    }    @Override    protected String doInBackground(URLWithParams... urls) {        return connect(urls[0].url, urls[0].namevaluePairs);    }    public static String connect(String url, List<NamevaluePair> pairs)    {        HttpClient httpclient = new DefaultHttpClient();        if(url == null)        { Log.d(TAG, "want to connect, but url is null");        }        else        { Log.d(TAG, "starting connect with url " + url);        }        if(pairs == null)        { Log.d(TAG, "want to connect, though pairs is null");        }        else        { Log.d(TAG, "starting connect with this many pairs: " + pairs.size()); for(NamevaluePair dog : pairs) {     Log.d(TAG, "example: " + dog.toString()); }        }        // Execute the request        HttpResponse response;        try { // Prepare a request object HttpPost httpPost = new HttpPost(url);  httpPost.setEntity(new UrlEnpredFormEntity(pairs)); response = httpclient.execute(httpPost); // Examine the response status Log.i(TAG,response.getStatusLine().toString()); BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8")); String json = reader.readLine(); return json;        } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace();        } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace();        }        return null;    }    @Override    protected void onPostExecute(String json ) {        getJSONListener.onRemoteCallComplete(json);    }    public interface GetJSonListener {        public void onRemoteCallComplete(String jsonFromNet);    }}

然后从我的主班这样叫

public class BookCatalog implements GetJSonListener {    private final String TAG = this.getClass().getSimpleName();    private String catalog_url = "URL";    private void getCatalogFromServer() {        URLWithParams mURLWithParams = new URLWithParams();        mURLWithParams.url = catalog_url;        try { JSonClient asyncPoster = new JSonClient(this); asyncPoster.execute(mURLWithParams);        } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace();        }    }    @Override    public void onRemoteCallComplete(String jsonBookCatalogList) {        Log.d(TAG, "received json catalog:");        Log.d(TAG, jsonBookCatalogList);    JSonObject bookCatalogResult;    try {        bookCatalogResult = (JSONObject) new JSonTokener(jsonBookCatalogList).nextValue();        JSonArray books = bookCatalogResult.getJSonArray("books");        if(books != null) { ArrayList<String> newBookOrdering = new ArrayList<String>(); int num_books = books.length(); BookCatalogEntry temp; DebugLog.d(TAG, "apparently we found " + Integer.toString(num_books) + " books."); for(int book_id = 0; book_id < num_books; book_id++) {     JSonObject book = books.getJSonObject(book_id);     String title = book.getString("title");     int version = book.getInt("price"); }        }    } catch (JSonException e) {        e.printStackTrace();    }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/449683.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号