我想了解Retrofit,因爲它似乎照顧了我目前與JSON請求和處理中遇到的很多問題。首先,我知道我們使用的方法是在接口內部定義的,同時做出簡單的請求來獲取數據,這很容易指定從url中檢索什麼以及基於所有必要的端點在着名的github示例中。通過改造提出最低限度的發佈請求
所以,如果我們在檢索信息形成GitHub的API,我們會首先創建所有必要的POJO模型,這樣的再定義的接口:
public interface GithubService {
@GET("users/{username}")
Observable<Github>getGithHubUser(@Path("username")String userName);
}
從上我們有一些主要活動像:
Retrofit retrofit = new Retrofit.Builder()
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("https://api.github.com/")
.build();
GithubService githubService = retrofit.create(GithubService.class);
Observable<Github> githubUser = githubService.getGithHubUser("usersName");
githubUser.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.map(user -> "Github Username: " + user.getName() + "\nUrl:" +user.getUrl() + "\nfollowing: "+ user.getHireable())
.subscribe(userInfo -> Log.d("Output", userInfo));
這裏我的問題是如何發送JSON的信息,如果該URL需要是這樣的:
"data={\"process\":\"procesNumber\", \"phone\":\"123456\"}"
基本上,爲了得到任何迴應形成我一直在做這個使用簡單okhttp服務器:
OkHttpClient client = new OkHttpClient();
RequestBody body = RequestBody.create(CREATE_MEDIA_TYPE, "data={\"process\":\"procesNumber\", \"phone\":\"123456\"}");
String ALLWAYS_API = "http://something something bla bla";
Request request = new Request.Builder()
.url("https://blablabla")
.post(body)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
... etc etc etc
}
據我瞭解,即使我需要創建一個表示需要爲數據的POJO類送到改造,沿着線的東西:發送請求
public class DataRequest {
final String proces;
final String phone;
DataRequest(String process, String phone) {
this.process = process;
this.phone = phone;
}
}
這將符合該信息,但如何將實際解析到接口的實現?
interface DataService {
@Post(not a clue what to place here)
DataRequest postJson(@Body how?)
}
我怎樣才能真正把它添加到改造的建設者?我使用的例子來自網絡上的不同論壇以及其他用戶提出的其他問題,這個例子特別有助於理解幾件事情:How to POST raw whole JSON in the body of a Retrofit request?但我仍然不明白事情的發展和一些事情其他問題和例子對於我需要做的事情來說太複雜了。
如果這真的是你的api所期望的,那麼改變你的api。 – njzk2
@ njzk2男子我希望我能......我希望......我......可以...... –
第一件事就是你需要使用'@ Field'註釋來發送形狀中的東西'data = ...' – njzk2