如何从空JSONObject获取数据? [英] How do i get data from null JSONObject?

查看:343
本文介绍了如何从空JSONObject获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我关于StackOverflow的第一个问题,对于这个问题的表述不好,我们深感抱歉.

This is my first Question on StackOverflow, So sorry for bad representation of the question.

这是logcat中的Json:

Here is Json in logcat :

I/Result is: null{"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":300,"main":"Drizzle","description":"light intensity drizzle","icon":"09d"}],"base":"stations","main":{"temp":280.32,"pressure":1012,"humidity":81,"temp_min":279.15,"temp_max":281.15},"visibility":10000,"wind":{"speed":4.1,"deg":80},"clouds":{"all":90},"dt":1485789600,"sys":{"type":1,"id":5091,"message":0.0103,"country":"GB","sunrise":1485762037,"sunset":1485794875},"id":2643743,"name":"London","cod":200}

这是我的代码:

public class MainActivity extends AppCompatActivity {
    String weather,id,result;

    public class DownloadTask extends AsyncTask<String,Void,String>{

        @Override
        protected String doInBackground(String... urls) {
            try {
                URL url = new URL(urls[0]);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                InputStream inputStream = httpURLConnection.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                int data = inputStreamReader.read();

                while(data != -1){
                    char count = (char) data;
                    result += count;
                    data = inputStreamReader.read();
                }

                return result;

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

           try {
                JSONObject jsonObject = new JSONObject(result);

                 String res = jsonObject.getString("coord");

                Log.i("Result is ",res);
            } catch (JSONException e) {
                e.printStackTrace();
           }


        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       // Toast.makeText(this, id, Toast.LENGTH_SHORT).show();

        DownloadTask task = new DownloadTask();
        task.execute("http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b1b15e88fa797225412429c1c50c122a1");

    }
}

这是例外:

W/System.err:org.json.JSONException:类型的值null org.json.JSONObject $ 1无法转换为JSONObject

W/System.err: org.json.JSONException: Value null of type org.json.JSONObject$1 cannot be converted to JSONObject

推荐答案

初始化结果字符串为空

String result="";

您的结果字符串是类变量,默认情况下,所有字符串都使用 =null

Your result String is class variable and all String are by default initialize with =null value

在下面的行中添加到 null+yourResponse

below line add to null+yourResponse

 result += count;

这篇关于如何从空JSONObject获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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