2015-02-06 122 views
0

你好我的Android项目是获取价值

  • 一个GPSTracker.java类这给纬度和经度
  • ,其从ADRESS API获取JSON一个RemoteFetch.java类在静止状态
  • 一个MainActivity.java 但是URL获得JSON是这样的:

    私有静态最后弦乐OPEN_WEATHER_MAP_API = “http://api.openweathermap.org/data/2.5/weather?“ +“q =%s &”+“lat =”+“& lon =”+“& units = metric”;

我的经度和纬度都在GPSTracker,但是当我得到他们来说,这不要让他们在静态格式不是我把这些价值观......

所以只要我将它们添加到我的openweatherapi url中,我有错误:

“非静态字段不能从静态上下文中引用”。

有没有办法将一个字符串/ int格式“转换”为静态格式?

如果您需要更多信息,请直接告诉我。

这里的一些代码

private void updateWeatherData(final String city){ 
     new Thread(){ 
      public void run(){ 

       final JSONObject json = RemoteFetch.getJSON(MainActivity.this,city); 
       if(json == null){ 
        handler.post(new Runnable(){ 
         public void run(){ 
          Toast.makeText(MainActivity.this.getApplicationContext(),R.string.place_not_found,Toast.LENGTH_LONG).show(); 
         } 
        }); 
       } else { 
        handler.post(new Runnable(){ 
         public void run(){ 
          renderWeather(json); 
         } 
        }); 
       } 
      } 
     }.start(); 
    } 
在RemoteFetch.java

private static final String OPEN_WEATHER_MAP_API = 
      "http://api.openweathermap.org/data/2.5/weather?" + "q=%s&" +"lat="+"&lon=" + "&units=metric"; 
    public static JSONObject getJSON(Context context, String city){ 
     try { 
      // gps.getLatitude(); 
      URL url = new URL(String.format(OPEN_WEATHER_MAP_API, city)); 
      HttpURLConnection connection = 
        (HttpURLConnection)url.openConnection(); 

      connection.addRequestProperty("x-api-key", 
        context.getString(R.string.open_weather_maps_app_id)); 

      BufferedReader reader = new BufferedReader(
        new InputStreamReader(connection.getInputStream())); 

      StringBuffer json = new StringBuffer(1024); 
      String tmp=""; 
      while((tmp=reader.readLine())!=null) 
       json.append(tmp).append("\n"); 
      reader.close(); 


      JSONObject data = new JSONObject(json.toString()); 
+0

你可以改变你的GPSTracker类获取坐标方法是静态的吗? – Xcihnegn 2015-02-06 10:28:55

回答

0

您试图访问的东西,是不是在你的范围(又名背景,但在语言方面,不要混淆它与类上下文)。因此,您需要将您的变量作为参数传递给静态方法,或者创建方法和实例方法(删除静态)。

编辑作进一步的解释:

在你的代码的某些部分(我想是不是你贴的部分),你正在使用的属性。在你的静态方法中你看不到那个属性。 “静态”意味着无论它是什么,它生活在类中,而非静态的东西属于该类的实例。虽然代码似乎在编写时位于同一位置,但它不会在运行时位于同一位置。这就是为什么当你在静态上下文中进行某些操作时(即在静态方法中),你无法看到实例的属性。

所以,你可以:

  • 把它作为一个参数,就像你与其他两个(背景下,市)一样,

  • ,或者您从您的toJSON删除“静态”的关键字方法。当心它是否在静态上下文的其他地方使用,因为它可能会引发编译错误,说现在这些其他上下文无法看到该方法!

此外,您可能想看看GSON https://sites.google.com/site/gson/gson-user-guide

一个在你的错误,阅读它,并尝试去了解它是如何工作的SO响应:"Non-static method cannot be referenced from a static context" error

而且在类成员http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html一些文档

我会建议你回顾一下基本的OOP概念,因为你遇到的这个错误是非常基本的。

+0

感谢您的回答,我真的不知道如何做到这一点,得到JSON的方法就是这样:public static JSONObject getJSON(Context context,String city){ try { URL url = new URL(String .format(OPEN_WEATHER_MAP_API,city));那么,我必须在String城市之后添加一些东西,比如双纬度?如果我删除静态我有“非静态方法”getJSON ...“不能从一个静态的上下文引用 – stmorray 2015-02-06 10:16:12

+0

我编辑我的答复更详细的解释和链接 – Logain 2015-02-06 10:24:07

0

您必须了解该类和该类的一个实例之间的区别。静态字段和方法连接到类本身,而不是它的实例。

由于根据您的代码,latitude and longitude are in the GPSTracker不是静态的(这些是实例变量),您将收到错误消息。

为了解决您的问题,您需要实例化类的实例(创建对象),以便运行时可以为实例保留内存并允许您访问实例变量(在您的案例中,latitude and longitude are in the GPSTracker)。下面的代码

尝试要访问latitude and longitude在GPSTracker(在MainActivity的onCreate()例如)

GPSTracker obj = new GPSTracker();

访问实例变量的纬度和经度使用obj如下 obj.latitudeobj.longitude。其他

2点考虑上述工作:

1.确保你import GPSTracker类文件要使用它(MainActicity.java为例),如果它是你的文件的包内。

2.如果GPSTracker类定义了一些其他的构造函数,您需要在创建对象时调用该构造函数。

+0

是的,这正是我正在做的,但作为这些值不是静态的.. – stmorray 2015-02-06 10:35:17

+0

请显示一些代码!! – AADProgramming 2015-02-06 10:50:33

+0

添加一些代码 – stmorray 2015-02-06 13:20:54