[android]我的应用程序不会添加到天气信息 TextView (JSON)

标签: json Android
发布时间: 2017/3/4 3:46:02
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我最近开始学习 JSON 在我的应用程序中使用。我发现天气 API (openweathermap.org),并用它对我的应用程序,该应用程序运行正常,但在按下的时 Button ,什么也没有发生.我的源代码︰

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

}

祝酒词和日志都没有工作。

解决方法 1:

你有在读取数据时出错。-1 的时候,你应该停止阅读不 1。

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

                data = reader.read();
            }
官方微信
官方QQ群
31647020