gpt4 book ai didi

java - 如何更改API请求?

转载 作者:太空宇宙 更新时间:2023-11-04 09:07:51 30 4
gpt4 key购买 nike

我是 Android 开发新手。我有一个来自 github here 的例子。此示例展示了一种与服务器交互以获取查询建议的方法。目前请求正文有以下形式:

{
"query": "mosc",
"count": 10
}

我需要像这样编辑正文:

{
"query": "mosc",
"from_bound": { "value": "region" },
"to_bound": { "value": "region" },
"count": 10
}

提出请求时仅向我显示区域,而不是完整地址。

一段代码,其中请求如果形成:DaDataRestClient.java

RestAdapter restAdapter = new RestAdapter.Builder()
.setLogLevel(BuildConfig.DEBUG ? RestAdapter.LogLevel.FULL : RestAdapter.LogLevel.NONE)
.setEndpoint(BASE_URL)
.setRequestInterceptor(new RequestInterceptor() {
@Override
public void intercept(RequestFacade request) {
request.addHeader("Content-Type", "application/json");
request.addHeader("Accept", "application/json");
request.addHeader("Authorization", "Token ".concat(BuildConfig.DADATA_API_KEY));
}
})
.setConverter(new GsonConverter(gson))
.build();
apiService = restAdapter.create(DaDataService.class);

和 ServerUtils.java:

if (queryRealmResults.size() == 0) {
RealmDaDataSuggestion suggestion = null;
try {
// Synchronously get the answer from DaData
suggestion = DaDataRestClient.getInstance().suggestSync(new DaDataBody(queryFromUser, 10));
...

和 DaDataBody 类:

public class DaDataBody {
private String query;
private int count;

public DaDataBody(String query, int count) {
this.query = query;
this.count = count;
}

public String getQuery() {
return query;
}

public void setQuery(String query) {
this.query = query;
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}
}

我已经花了很多时间,但对我来说没有任何作用:(请帮助我,我该如何编辑此请求?提前致谢!

PS:最终我想创建两个 EditText,一个用于区域,第二个用于城市。

P.P.S.:抱歉我的英语)

最佳答案

如下所示更新您的模型类

public class DaDataBody{

@SerializedName("from_bound")
private Property fromBound;

@SerializedName("query")
private String query;

@SerializedName("count")
private int count;

@SerializedName("to_bound")
private Property toBound;

public void setFromBound(Property fromBound){
this.fromBound = fromBound;
}

public Property getFromBound(){
return fromBound;
}

public void setQuery(String query){
this.query = query;
}

public String getQuery(){
return query;
}

public void setCount(int count){
this.count = count;
}

public int getCount(){
return count;
}

public void setToBound(Property toBound){
this.toBound = toBound;
}

public Property getToBound(){
return toBound;
}

public class Property {

@SerializedName("value")
private String value;

public void setValue(String value){
this.value = value;
}

public String getValue(){
return value;
}


}
}

为 API 调用创建请求正文

        DaDataBody body=new DaDataBody();
body.setQuery("your query");
body.setCount(10);
//set if you have value for tobound
body.setToBound(new Property("your value"));

//set if you have value for frombound
body.setFromBound(new Property("your value"));

如果没有值,请不要为 tobound 和 frombound 设置值或设置 null。因为 gson 在生成 json 时不包含空值键的键和值。

关于java - 如何更改API请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60015324/

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