java - 如何从 null JSONObject 获取数据?

标签 java android json exception

这是我在 StackOverflow 上的第一个问题,很抱歉问题的表述不准确。

这是 logcat 中的 Json :

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");

    }
}

这是异常(exception):

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

最佳答案

用空初始化结果字符串

String result="";

您的结果字符串是类变量,所有字符串默认都使用 =null 值初始化

下面一行添加到null+yourResponse

 result += count;

关于java - 如何从 null JSONObject 获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46127487/

相关文章:

android - Navigation Drawer Android 最佳实践

javascript - 为什么 json 响应有方括号

Android Intent 到 Amazon 应用程序

java - 延迟加载库?

javascript - Selenium:如何检查匿名函数的代码

java - java中的Mongo聚合

java - 在 Samsung Galaxy S6 android 7 上立即取消通知

javascript - 使用 lodash 或类似的库,如何按最接近的结果对集合进行排序?

ruby-on-rails - 将变量从 Controller 传递到 Rails 中的序列化程序

java - 在Android中播放指定时间的音频