我的应用程序不会向TextView(JSON)添加天气信息

时间:2017-02-10 16:38:46

标签: android json

我最近开始学习JSON以在我的应用中使用。我找到了一个天气API(openweathermap.org)并在我的应用程序上使用它。应用程序运行正常,但当我按下按钮时,没有任何反应。我的源代码:

import android.content.Context;
import android.hardware.input.InputManager;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.*;
import java.net.*;
import java.util.*;
import android.util.*;
import android.view.*;
import android.view.inputmethod.InputMethodManager;
import android.widget.*;
import org.json.*;


public class MainActivity extends AppCompatActivity {

    EditText city;
    TextView weather, description;

    DownloadTask DownloadTask;
    public void showWeather (View view)
    {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(city.getWindowToken(), 0);

        try
        {
            String encodedCityName = URLEncoder.encode(city.getText().toString(), "UTF-8");
            DownloadTask = new DownloadTask();
            DownloadTask.execute("http://api.openweathermap.org/data/2.5/weather?q=" + encodedCityName + "&appid=812f300ec742971975bbde9a2e0ac0c1");
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
            Toast.makeText(getApplicationContext(), "Error!", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        city = (EditText) findViewById(R.id.city);
        weather = (TextView) findViewById(R.id.weather);
        description = (TextView) findViewById(R.id.description);
    }

    public class DownloadTask extends AsyncTask<String, Void, String>
    {
        @Override
        protected String doInBackground(String... urls) {
            String result = "";
            URL url;
            HttpURLConnection connection =  null;

            try
            {
                url = new URL(urls[0]);
                connection = (HttpURLConnection) url.openConnection();

                InputStream in = connection.getInputStream();
                InputStreamReader reader = new InputStreamReader(in);

                int data = reader.read();

                while (data != 1)
                {
                    char current = (char) data;
                    result += current;

                    data = reader.read();
                }
                return result;

            }
            catch (Exception e)
            {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "Error!", Toast.LENGTH_LONG);

            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {

            super.onPostExecute(result);
            Log.i("Weather content", result);
            try
            {
                JSONObject jsonObject = new JSONObject(result);
                String weatherInfo = jsonObject.getString("weather");
                Log.i("Weather Content", weatherInfo);

                JSONArray jsonArray = new JSONArray(weatherInfo);

                String getMain = "";
                String getDescription = "";

                for (int i = 0; i < jsonArray.length(); i++)
                {
                    JSONObject jsonPart = jsonArray.getJSONObject(i);

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

                    Log.i("Main", getMain);

                }
                if (getMain != "" && getDescription != "")
                {
                    weather.setText(getMain);
                    description.setText(getDescription);
                }
            }
            catch (JSONException e)
            {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "Error!", Toast.LENGTH_LONG);
            }
        }
    }

}

Toasts和Logs都不起作用。

1 个答案:

答案 0 :(得分:0)

您在读取数据时出错。当得-1而不是1时你应该停止阅读。

         while (data != -1)
            {
                char current = (char) data;
                result += current;

                data = reader.read();
            }