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

在Android上从URL简单解析JSON并显示在列表视图中

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

在Android上从URL简单解析JSON并显示在列表视图中

您可以使用

AsyncTask
,您必须进行自定义以满足您的需求,但是类似以下内容


异步任务有三种主要方法:

  1. onPreExecute()
    -最常用于设置和启动进度对话框

  2. doInBackground()
    -建立连接并从服务器接收响应(请勿尝试将响应值分配给GUI元素,这是一个常见错误,无法在后台线程中完成)。

  3. onPostExecute()
    -这里我们不在后台线程中,因此我们可以使用响应数据进行用户界面操作,或者简单地将响应分配给特定的变量类型。


首先,我们将启动类,初始化一个

String
以将结果保存在方法外部但在类内部,然后运行
onPreExecute()
方法以建立简单的进度对话框。

class MyAsyncTask extends AsyncTask<String, String, Void> {    private ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);    InputStream inputStream = null;    String result = "";    protected void onPreExecute() {        progressDialog.setMessage("Downloading your data...");        progressDialog.show();        progressDialog.setonCancelListener(new onCancelListener() { public void onCancel(DialogInterface arg0) {     MyAsyncTask.this.cancel(true); }        });    }

然后,我们需要建立连接以及我们如何处理响应:

    @Override    protected Void doInBackground(String... params) {        String url_select = "http://yoururlhere.com";        ArrayList<NamevaluePair> param = new ArrayList<NamevaluePair>();        try { // Set up HTTP post // HttpClient is more then less deprecated. Need to change to URLConnection HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url_select); httpPost.setEntity(new UrlEnpredFormEntity(param)); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); // Read content & Log inputStream = httpEntity.getContent();        } catch (UnsupportedEncodingException e1) { Log.e("UnsupportedEncodingException", e1.toString()); e1.printStackTrace();        } catch (ClientProtocolException e2) { Log.e("ClientProtocolException", e2.toString()); e2.printStackTrace();        } catch (IllegalStateException e3) { Log.e("IllegalStateException", e3.toString()); e3.printStackTrace();        } catch (IOException e4) { Log.e("IOException", e4.toString()); e4.printStackTrace();        }        // Convert response to string using String Builder        try { BufferedReader bReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"), 8); StringBuilder sBuilder = new StringBuilder(); String line = null; while ((line = bReader.readLine()) != null) {     sBuilder.append(line + "n"); } inputStream.close(); result = sBuilder.toString();        } catch (Exception e) { Log.e("StringBuilding & BufferedReader", "Error converting result " + e.toString());        }    } // protected Void doInBackground(String... params)

最后,在这里我们将分析返回值,在此示例中,它是一个JSON数组,然后关闭该对话框:

    protected void onPostExecute(Void v) {        //parse JSON data        try { JSonArray jArray = new JSonArray(result);     for(i=0; i < jArray.length(); i++) {     JSonObject jObject = jArray.getJSonObject(i);     String name = jObject.getString("name");     String tab1_text = jObject.getString("tab1_text");     int active = jObject.getInt("active"); } // End Loop this.progressDialog.dismiss();        } catch (JSonException e) { Log.e("JSONException", "Error: " + e.toString());        } // catch (JSonException e)    } // protected void onPostExecute(Void v)} //class MyAsyncTask extends AsyncTask<String, String, Void>


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

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

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