2016-12-27 80 views
0

檢索使用改裝的數據我與改造工作,從網址,以獲取JSON數據這樣http://api.openweathermap.org/data/2.5/weather?id=1277333&appid=00f05ed7a5400d4d7765e69330e28ab4在安卓

我的接口類是

public interface RetrofitObjectAPI { 

String API_KEY="00f05ed7a5400d4d7765e69330e28ab4"; 

@GET("data/2.5/weather?q=Bangalore,india&appid="+API_KEY) 
Call<Weather> getWeatherDetails(); 
} 

我Weather.java

public class Weather { 

String name; 
String temp; 
String pressure; 
String humidity; 
String wind; 
String coord; 

public String getName() { 
    return name; 
} 

public String getTemp() { 
    return temp; 
} 


public String getPressure() { 
    return pressure; 
} 


public String getHumidity() { 
    return humidity; 
} 


public String getWind() { 
    return wind; 
} 


public String getCoord() { 
    return coord; 
} 
} 

我的MainActivity.java

public class MainActivity extends AppCompatActivity { 

String url = "http://api.openweathermap.org/"; 
TextView cityname,temperature,pressure,humidity,wind,coord; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    cityname=(TextView)findViewById(R.id.cityText); 
    temperature=(TextView)findViewById(R.id.temp); 
    pressure=(TextView)findViewById(R.id.press); 
    humidity=(TextView)findViewById(R.id.hum); 
    wind=(TextView)findViewById(R.id.windSpeed); 
    coord=(TextView)findViewById(R.id.coord); 


    Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl(url) 
      .addConverterFactory(GsonConverterFactory.create()) 
      .build(); 

    RetrofitObjectAPI service = retrofit.create(RetrofitObjectAPI.class); 

    Call<Weather> call = service.getWeatherDetails(); 

    call.enqueue(new Callback<Weather>() { 
     @Override 
     public void onResponse(Response<Weather> response, Retrofit retrofit) { 

      try { 

       cityname.setText(response.body().getName()); 
       temperature.setText(response.body().getTemp()); 
       pressure.setText(response.body().getPressure()); 
       humidity.setText(response.body().getHumidity()); 
       wind.setText(response.body().getWind()); 
       coord.setText(response.body().getCoord()); 

      } catch (Exception e) { 
       Log.d("onResponse", "There is an error"); 
       e.printStackTrace(); 
      } 

     } 

     @Override 
     public void onFailure(Throwable t) { 
      Log.d("onFailure", t.toString()); 
     } 
    }); 


} 
} 

我無法獲取數據。請幫助我,因爲我是翻新新手。

我logcat的

12-27 15:50:48.975 22956-22956/com.example.praveen.weather I/art:Late-enabling -Xcheck:jni 
12-27 15:50:49.094 22956-22956/com.example.praveen.weather I/InstantRun: Instant Run Runtime started. Android package is com.example.praveen.weather, real application class is null. 
12-27 15:50:49.129 22956-22956/com.example.praveen.weather W/art: Failed to find OatDexFile for DexFile /data/data/com.example.praveen.weather/files/instant-run/dex/slice-slice_6-classes.dex (canonical path /data/data/com.example.praveen.weather/files/instant-run/dex/slice-slice_6-classes.dex) with checksum 0xb063b4c2 in OatFile /data/data/com.example.praveen.weather/cache/slice-slice_6-classes.dex 
12-27 15:50:50.046 22956-22956/com.example.praveen.weather W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 
12-27 15:50:50.551 22956-22956/com.example.praveen.weather I/ViewRootImpl: CPU Rendering VSync enable = true 
12-27 15:50:50.554 22956-23012/com.example.praveen.weather D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true 
12-27 15:50:50.564 22956-22956/com.example.praveen.weather D/Atlas: Validating map... 
12-27 15:50:50.606 22956-23012/com.example.praveen.weather I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BR.1.2.3_RB1.05.01.00.036.053_msm8909_LA.BR.1.2.3_RB1__release_AU (Iac7c2e2837) 
                    OpenGL ES Shader Compiler Version: E031.25.03.04 
                    Build Date: 09/01/15 Tue 
                    Local Branch: mybranch13650421 
                    Remote Branch: quic/LA.BR.1.2.3_rb1.76 
                    Local Patches: NONE 
                    Reconstruct Branch: AU_LINUX_ANDROID_LA.BR.1.2.3_RB1.05.01.00.036.053 + 7c4888a + 22ca218 + 855d166 + 95575a0 + 4193eef + 64d916f + 88f4cbe + c272012 + 046ce63 + 9b2bddc + NOTHING 
12-27 15:50:50.607 22956-23012/com.example.praveen.weather I/OpenGLRenderer: Initialized EGL, version 1.4 
12-27 15:50:50.616 22956-23012/com.example.praveen.weather D/OpenGLRenderer: Enabling debug mode 0 
12-27 15:50:50.753 22956-22956/com.example.praveen.weather D/onFailure: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 11 path $.coord 
12-27 15:50:50.753 22956-22956/com.example.praveen.weather I/Timeline: Timeline: Activity_idle id: [email protected] time:40949345 

沒有錯誤。

+0

你得到任何錯誤,請分享你的日誌貓。 – Meenal

+1

您的響應對象與api返回的響應不一樣 – darwin

+0

您可以給我示例代碼,我必須給出響應,根據我的要求 –

回答

3

wind不是字符串,所以Gson不能將它轉換爲String。你必須創建自己的對象喜歡你天氣

class Wind { 
    float speed; 
    int deg; 
    public Wind(float speed, int deg){this.speed = speed; this.deg = deg;} 
    public float getSpeed(){return speed;} 
    public void setSpeed(float speed){this.speed = speed;} 
    public int getDeg(){return deg;} 
    public void setDeg(int deg){this.deg = deg;} 
} 
// Then use this in Weather class 
Wind wind; // Instead of String wind 
// Whenever you want to get wind speed with your response from server 
float windSpeed = response.getWind().getSpeed(); 

同爲別人做了什麼與。希望它能幫助:)

+0

感謝您的答覆。我如何在迴應中調用它們並設置爲textview –

+0

'Wind wind = response.body()。getWind();' 'float speed = wind.getSpeed();' 當然,您需要編寫這些方法由你自己。 – RadekJ

+0

編輯答案。 @RadekJ是正確的 – Nhan

2

你有什麼期待有String wind;在你的反應,如果它是:

"wind": { 
     "speed": 3.6, 
     "deg": 60 
}, 

你應該寫類「風」,並在其中你將有兩個字段:

double speed; 
int deg; 

然後在您的Weather類別中,名爲'wind'的字段將是Wind而不是String

+0

在我的迴應如何調用這些值並設置爲textview –

0

這裏是方法:

etSearch.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) { 

     } 

     @Override 
     public void onTextChanged(final CharSequence s, final int start, final int before, final int count) { 

     } 

     @Override 
     public void afterTextChanged(final Editable s) { 
      if (s.length() == 0) { 
       setListData(notesViewslist); 
      } else if (s.length() > 0) { 
       performLocalSearch(); 
      } 
     } 
    }); 
} 

private void performLocalSearch() { 
    if (etSearch.getText().toString().trim().length() == 0) { 
     setListData(notesViewslist); 
    } else { 
     final String search = etSearch.getText().toString().trim().toLowerCase(); 
     final ArrayList<NotesViews> mSearchList = new ArrayList<NotesViews>(); 
     for (NotesViews mServices : notesViewslist) { 
      if (mServices.getTitle() != null && mServices.getTitle().toLowerCase().contains(search)) { 
       mSearchList.add(mServices); 
      } 
      else if (mServices.getCreateddate() != null && mServices.getCreateddate().toLowerCase().contains(search)) 
      { 
       mSearchList.add(mServices); 
      } 
     } 
     setListData(mSearchList); 
    } 
} 
+0

請將解釋添加到您的代碼以便更好地理解 – Ibo