【问题标题】:to retrieve data using retrofit in android在 android 中使用改造检索数据
【发布时间】:2016-12-27 10:14:19
【问题描述】:

我正在使用改造从 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();
}

我的 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());
        }
    });


}
}

我无法获取数据。请帮助我,因为我是改装新手。

我的日志猫

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

没有错误。

【问题讨论】:

  • 您是否遇到任何错误,请分享您的日志猫。
  • 你的响应对象和api返回的响应不一样
  • 你能给我示例代码吗?我必须根据我的要求给出响应
  • 您可以使用jsonschema2pojo.org 将json字符串转换为POJO类并用作您的响应类
  • 请分享您的天气课程。响应似乎有误

标签: android json retrofit openweathermap


【解决方案1】:

wind 不是字符串,因此 Gson 无法将其强制转换为字符串。您必须像使用 Weather

一样创建自己的对象
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();

其他人也一样。希望对你有帮助:)

【讨论】:

  • 感谢您的回复。我必须如何调用它们以响应并设置为 textview
  • Wind wind = response.body().getWind(); float speed = wind.getSpeed(); 当然这些方法需要自己写。
【解决方案2】:

如果是,您希望String wind; 在您的回复中包含什么:

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

您应该编写“Wind”类,其中包含两个字段:

double speed;
int deg;

然后在您的 Weather 类字段中名为“风”的类型将是 Wind 而不是 String

【讨论】:

  • 在我的回复中如何调用这些值并设置为 textview
【解决方案3】:

方法如下:

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);
    }
}

【讨论】:

  • 请在您的代码中添加解释以便更好地理解
猜你喜欢
  • 2021-07-14
  • 2021-04-30
  • 1970-01-01
  • 1970-01-01
  • 2021-04-26
  • 2019-06-14
  • 2017-10-10
  • 2014-12-17
  • 2017-07-07
相关资源
最近更新 更多