[英]No HTTP request is send from retrofit /Android/
我正在嘗試使用Retrofit從我的Android項目向API發送數據。 一切似乎沒有錯誤,但沒有http請求離開應用程序。 我可以通過Wireshark篩選和API控制台日志來確認這一點。 這是我的應用程序的這部分的示例偽代碼:
// sample code
findViewById(R.id.submit_btn).setOnClickListener(this);
@Override
public void onClick(View v) {
int i = v.getId();
if (i == R.id.submit_btn){
Intent intent = new Intent(CurrentActivity.this, HomeActivity.class);
// myObj is class storing several values and it is defined in separate class
MyObj obj = new MyObj(** some attributes here **);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://api.address")
.client(new OkHttpClient())
.addConverterFactory(GsonConverterFactory.create())
.build();
MyAPI api = retrofit.create(MyAPI.class);
Call<Void> upload = api.newObj(obj);
startActivity(intent);
finish();
}
不知道我錯過了什么。 有任何想法嗎?
這里的PS是應用程序用於此部分的依賴項:
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.okhttp3:okhttp:3.0.1'
這只會准備請求,而不是發送請求。
Call<Void> upload = api.newObj(obj);
嘗試制作upload.enqueue()
你永遠不會入隊或執行調用,執行異步調用服務器使用upload.enqueue(new CallBack here)
執行同步立即使用upload.execute()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.