小编Sma*_*ngh的帖子

我如何从null JSONObject获取数据?

这是我关于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}
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

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) { …
Run Code Online (Sandbox Code Playgroud)

java android json exception-handling

0
推荐指数
1
解决办法
338
查看次数

标签 统计

android ×1

exception-handling ×1

java ×1

json ×1