java - 如何从 void 方法传递变量?

标签 java variables methods void transfer

我需要从以下位置获取纬度和经度:public void onLocationChanged(Location location) 并将其传输到另一个方法(JSON 解析器),以便将其应用于天气 API 的 url。对此,小伙伴们有什么建议呢?我对此还很陌生,如果有一个例子也将不胜感激。

@Override
public void onLocationChanged(Location location) {
    url = "https://api.openweathermap.org/data/2.5/weather?lat=" + location.getLatitude() + "&lon=" + location.getLongitude() + "&appid=5d8fea5f1c9cdfe8af473504e5f9002a";
    Log.i("api", url);

    //double lat = location.getLatitude();
   // double lng = location.getLongitude();

}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {

}

@Override
public void onProviderEnabled(String s) {

}

@Override
public void onProviderDisabled(String s) {

}  

我需要的是从 Location 方法到 JsonObjectRequest 的 URL

private void jsonParse() {


        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {

                    JSONObject weatherData = response.getJSONObject("main");
                    double temp = weatherData.getDouble("temp");
                    double tempHigh = weatherData.getDouble("temp_max");
                    double tempLow = weatherData.getDouble("temp_min");
                    double humidity = weatherData.getDouble("humidity");


                    Log.i("JSONWork", String.valueOf(weatherData));
                    TV_temp_max.append("Highest Temperature: " + tempHigh);
                    TV_temp_min.append("Lowest Temperature: " + tempLow);
                    TV_temp.append("Current Temperature: " + temp);
                    TV_humidity.append("Humidity: %" + humidity);

                    JSONObject locationData = response.getJSONObject("sys");
                    String country = locationData.getString("country");
                    String city = locationData.getString("name");
                    TV_country.append(country);
                    TV_city.append(city);

                } catch (JSONException e) {
                    Log.i("error", "JSON is not working");
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();


            }
        });

        mQueue.add(request);
    }

最佳答案

我猜测当位置已更新为作为参数给出的位置时,会调用 onLocationChanged(Location location) 。因此,如果您想进一步处理它,只需从那里获取即可。

public void onLocationChanged(Location loc) {
    String jsonLoc = createJson(loc);
    sendToWeatherApi(jsonLoc);
}

关于java - 如何从 void 方法传递变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50545151/

相关文章:

java - 冒号分隔值的正则表达式是什么?

c++ - 定义类中声明的变量的范围(C++)?

oop - 使用反射调用方法并返回值

c# - 您可以停止通过其调用的方法执行的按钮单击事件吗?

ruby-on-rails - Rails - 在方法内部引用当前模型实例的正确方法?

java - 黑莓模拟器不创建 COD 文件

java - 在 spring-security @PreAuthorize 中删除这个未使用的方法参数

java - 空套接字输入流

Jquery,从函数访问变量

mysql - MySQL 中带有条件变量的存储过程