如何在android studio中使用jsontoken? [英] How to use jsontoken in android studio?

查看:328
本文介绍了如何在android studio中使用jsontoken?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个web服务来显示灯的状态,我正在尝试使用JSONObject来检索状态值,当我使用JSONToken返回JSONObjact值时,我收到null。



因为有一个JSONException。



这是MyAsyncTasks()的代码:

I've a webservice to display the status of the light and I'm trying to retrive the status value using JSONObject and when I use JSONToken to return the JSONObjact value I receive "null".

As there is a JSONException.

Here is my code for MyAsyncTasks():

public class MyAsyncTasks extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... strings) {
            HttpHandler sh = new HttpHandler();

            String current = "";
            try {
                URL ApiUrl;
                HttpURLConnection urlConnection = null;
                try {
                    ApiUrl = new URL(apiUrl);
                    urlConnection = (HttpURLConnection) ApiUrl.openConnection();
                    instring = urlConnection.getInputStream();
                    progressDialog.dismiss();
         
                    JSONObject  c=  parseInputStream(instring);

                    try {
                        for (int i = 0; i < c.length(); i++)
                            status = (int) c.get(String.valueOf(Integer.parseInt(String.valueOf(status))));

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    if (status == 0) {
                        Button1.setBackgroundColor(Color.RED);
                    } else {
                        Button1.setBackgroundColor(Color.GREEN);
                    }

                    return "";

                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (urlConnection != null) {
                        urlConnection.disconnect();
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
            return"";
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            progressDialog = new ProgressDialog(MainActivity.this);
            progressDialog.setMessage("Please Wait");
            progressDialog.setCancelable(false);

        }


        InputStream instring;

        public static JSONObject  parseInputStream(InputStream inputStream) {

            BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder StrBuilder = new StringBuilder();
            JSONObject  jsonObject =  null;
            String currentline;
            try {
                while ((currentline = streamReader.readLine()) != null) {
                    StrBuilder.append(currentline);
                }

                JSONTokener jsonTokener = new JSONTokener(StrBuilder.toString());
                jsonObject = new JSONObject (jsonTokener);
            } catch (IOException error) {

            } catch (JSONException e) {
                e.printStackTrace();
            }

            
            return jsonObject;


        }

我的服务如下:

<string xmlns="http://tempuri.org/">
[ { "id": 3, "name": "light1", "status": "1" }, { "id": 4, "name": "light2", "status": "1" }, { "id": 5, "name": "light3", "status": "1" }, { "id": 6, "name": "light4", "status": "1" } ]





我的尝试:



由于我是Android新手,我不知道必须使用什么代替JSONToken。



What I have tried:

As I'm new to Android, I've no idea what has to be used insteadof JSONToken.

推荐答案

你必须投 - 将指定值转换为目标值。

搜索不同类型的json数据结构。

json数组与json对象不同。
you have to cast - convert assigned value to target value .
search for different type of json data structure .
json array is different from json object.


这篇关于如何在android studio中使用jsontoken?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆