简单的天气应用程序不起作用?

时间:2016-10-09 13:36:21

标签: java android json

我正在尝试显示来自opneweathermap的json数据的主要和描述。没有任何事情发生,但我没有错误。

我不会包含该链接。

这是我的MainActivity类

public class MainActivity extends AppCompatActivity {

    private Button mButton;
    private TextView mTextView;

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

        mButton = (Button) findViewById(R.id.button);
        mTextView = (TextView) findViewById(R.id.textView2);

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                DownloadTask task = new DownloadTask();
                task.execute("Link");
            }
        });
    }


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

        @Override
        protected String doInBackground(String... urls) {

            String result = "";
            URL url;
            HttpURLConnection urlConnection = null;

            try {
                url = new URL(urls[0]);

                urlConnection = (HttpURLConnection) url.openConnection();

                InputStream inputStream = urlConnection.getInputStream();

                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);

                int data = inputStreamReader.read();

                while(data != -1){

                    char current = (char) data;

                    result+=current;

                    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 {

                String message = "";

                JSONObject jsonObject = new JSONObject(result);

                String weatherInfo = jsonObject.getString("weather");

                Toast.makeText(getApplicationContext(), "Soem"+message,Toast.LENGTH_LONG).show();

                Log.i("Weather content", weatherInfo);

                JSONArray array = new JSONArray(weatherInfo);

                for(int i=0;i<array.length();i++){

                    JSONObject jsonPart =  array.getJSONObject(i);

                    String main = "";
                    String description = "";

                    main = jsonPart.getString("main");
                    description = jsonPart.getString("description");

                    if(main != "" && description != ""){

                        message += main + ": " + description + "\r\n";

                    }
                }

                if(message != ""){
                    mTextView.setText(message);
                }

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

我已经包含了使用权限,我有TextView和Button,它将在我的布局文件中显示json中的数据。

0 个答案:

没有答案