我正在使用改造来从Url获取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();
}
My 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: android.os.BinderProxy@d717c04 time:40949345
没有错误。
答案 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();
其他人也一样。希望它有所帮助:)
答案 1 :(得分:2)
如果是,您希望在回复 "wind": {
"speed": 3.6,
"deg": 60
},
中得到什么:
double speed;
int deg;
你应该写“风”课,在其中你会有两个字段:
Wind
然后在名为“wind”的Weather类字段中,类型为String
,而不是{{1}}。
答案 2 :(得分: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);
}
}