【问题标题】:How to add city selection to weather app?如何将城市选择添加到天气应用程序?
【发布时间】:2017-08-25 14:57:36
【问题描述】:

我的天气应用使用 Openweathermap API。但我只能得到我输入代码的城市的预测。

我想添加一个选项,以便用户输入他的城市名称并获取它的天气预报。

我在布局中添加了一个 EditText,但不知道如何从中获取数据并将其用作城市名称输入。

这是我的代码:

package com.hamed.myapplication;

import android.content.Context;
import android.util.Log;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.hamed.myapplication.view.dataModel.WeatherInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Hamed on 8/20/2017.
 */

public class ApiService {
    private static final String TAG = "ApiService";
    private Context context;


    public ApiService (Context context){
        this.context=context;
    }

    public void getCurrentWeather(final OnWeatherInfoRecieved onWeatherInfoRecieved, String cityName){
        JsonObjectRequest request=new JsonObjectRequest(Request.Method.GET,
                "http://api.openweathermap.org/data/2.5/weather?q=ahvaz&apikey=01a477912e47daf2010808cc62015829",
                null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.i(TAG, "onResponse: "+response.toString());
                onWeatherInfoRecieved.onRecieved(parseResponseToWeatherInfo(response));

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "onErrorResponse: "+error.toString());
                onWeatherInfoRecieved.onRecieved(null);

            }
        });
        request.setRetryPolicy(new DefaultRetryPolicy(8000,DefaultRetryPolicy.DEFAULT_MAX_RETRIES,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        RequestQueue requestQueue= Volley.newRequestQueue(context);
        requestQueue.add(request);
    }

    private WeatherInfo parseResponseToWeatherInfo(JSONObject response){
        WeatherInfo weatherInfo= new WeatherInfo();
        try {
            JSONArray weatherJsonArray= response.getJSONArray("weather");

            JSONObject weatherJsonObject= weatherJsonArray.getJSONObject(0);
            weatherInfo.setWeatherName(weatherJsonObject.getString("main"));
            weatherInfo.setWeatherName(weatherJsonObject.getString("description"));

            JSONObject mainJsonObject=response.getJSONObject("main");
            weatherInfo.setWeatherTemperature((float)mainJsonObject.getDouble("temp"));
            weatherInfo.setHumidity(mainJsonObject.getInt("humidity"));
            weatherInfo.setPressure(mainJsonObject.getInt("pressure"));
            weatherInfo.setMinTemperature((float)mainJsonObject.getDouble("temp_min"));
            weatherInfo.setMaxTemperature((float)mainJsonObject.getDouble("temp_max"));

            JSONObject windJsonObject=response.getJSONObject("wind");
            weatherInfo.setWindSpeed((float)windJsonObject.getDouble("speed"));
            weatherInfo.setWindDegree((float)windJsonObject.getDouble("deg"));

            return weatherInfo;

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

            return null;
        }


    }

    public interface OnWeatherInfoRecieved {
        void onRecieved(WeatherInfo weatherInfo);
    }
}

编辑:

我的应用有一个从服务器请求数据的按钮。我希望用户在 EditText 中输入他的城市名称,并且 API 地址在其中使用该城市名称......

有人告诉我,我必须将 EditText 值用作 QueryString 并将其提供给 API。

有人知道怎么做吗?

【问题讨论】:

标签: java android weather


【解决方案1】:

从 EditText 中获取价值:

EditText edit = (EditText) view.findViewById("myEditText");
String city = edit.getText().toString();

这就是你要找的吗?

【讨论】:

  • 是的,但是如何在我的代码中使用该值作为城市名称?
【解决方案2】:

要添加城市选择,您必须调用远程 openweathermap 服务并检索城市。

Openweathermap API,您可以参考this tutorial,其中详细说明了如何在您的天气应用中添加城市搜索选项。它的最终源代码位于 Github here。您可以查看示例应用程序源代码中专门针对城市选择的XML fileJava Class

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-15
    相关资源
    最近更新 更多