gpt4 book ai didi

java - android - 使用改造来获取数据,但不解析为对象

转载 作者:行者123 更新时间:2023-12-02 03:46:37 24 4
gpt4 key购买 nike

我是改造新手。我正在尝试使用 Retrofit 查询 OpenWeatherMap 并返回一些天气数据。

这是我的一些代码:

public static void getCurForecast(final Resources resources, final String zipCode, final String countryCode, final ForecastListener listener) {
new AsyncTask<Void, Void, CurForecastResponse>() {

@Override
protected CurForecastResponse doInBackground(Void... params) {
Log.d("TAG","in CurForecastResponse");
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("http://api.openweathermap.org")
.build();
//String zipCode = "78613";
//String countryCode = "us";
ForecastRequest service = restAdapter.create(ForecastRequest.class);
try {
String zipCodeFormat = zipCode + ",us";
Log.d("TAG","zipCodeFormat: " + zipCodeFormat);
CurForecastResponse temp = service.getCurForecast(zipCodeFormat, resources.getString(R.string.open_weather_api_key));
if(temp == null){
Log.d("TAG","TEMP = NULL!");
}
else {
Log.d("TAG","Weather: " + temp.name);
}
return temp;//service.getCurForecast(zipCodeFormat, resources.getString(R.string.open_weather_api_key));
} catch (RetrofitError error) {
Log.w("ForecastModule", "Forecast error: " + error.getMessage());
return null;
}
};

这是 JSON 对象的一部分:

public class CurForecastResponse {

public int cod;
public String base;
public String name;
public ForecastCurrently currently;
public ForecastHourly hourly;
public Weather main;

public CurForecastResponse() {
Log.d("TAG","CurForecastResponse Constructor");
}
public class weather {
int id;
String main;
String description;
String icon;
public weather(int id, String main, String description, String icon) {
this.id = id;
this.main = main;
this.description = description;
this.icon = icon;
}
public int getId() {return id; }
public String getDescription() {return description; }
public String getIcon() {return icon; }
public String getMain() {return main; }
}

这是 ForecastRequest 类

public interface ForecastRequest {

//Open Weather Forecast API
@GET("/data/2.5/weather?")
CurForecastResponse getCurForecast(@Query("zip") String zipCode,
@Query("apiKey") String apiKey);
}

这是 JSON 响应的示例

    {"coord":{"lon":145.77,"lat":-16.92},
"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],
"base":"cmc stations",
"main":{"temp":305.15,"pressure":1013,"humidity":52,"temp_min":305.15,"temp_max":305.15},"wind":{"speed":5.7,"deg":40},"clouds":{"all":20},"dt":1459137600,"sys":{"type":1,"id":8166,"message":0.0092,"country":"AU","sunrise":1459110134,"sunset":1459153277},"id":2172797,"name":"Cairns","cod":200}

我在将数据输入天气类时遇到一些问题。

最佳答案

在 Retrofit2 中,您应该用 call 包装您的返回值。

Call instances can be executed either synchronously or asynchronously. Each instance can only be used once, but calling clone() will create a new instance that can be used.

来自其 github 存储库的快速示例是:

public final class SimpleService {

public static final String API_URL = "https://api.github.com";

public static class Contributor {
public final String login;
public final int contributions;

public Contributor(String login, int contributions) {
this.login = login;
this.contributions = contributions;
}
}

public interface GitHub {
@GET("/repos/{owner}/{repo}/contributors")
Call<List<Contributor>> contributors(
@Path("owner") String owner,
@Path("repo") String repo);
}

public static void main(String... args) throws IOException {
// Create a very simple REST adapter which points the GitHub API.
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();

// Create an instance of our GitHub API interface.
GitHub github = retrofit.create(GitHub.class);

// Create a call instance for looking up Retrofit contributors.
Call<List<Contributor>> call = github.contributors("square", "retrofit");

// Fetch and print a list of the contributors to the library.
List<Contributor> contributors = call.execute().body();
for (Contributor contributor : contributors) {
System.out.println(contributor.login + " (" + contributor.contributions + ")");
}
}
}

注意这一行:

 List<Contributor> contributors = call.execute().body();

运行 synchronously 。对于 asynchronous调用使用call.enqueue并重写回调方法。

关于java - android - 使用改造来获取数据,但不解析为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36242387/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com