我正在尝试使用 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();
}
不知道我错过了什么。有什么想法吗?
附注以下是应用程序在这部分使用的依赖项:
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()
关于java - 没有从 Retrofit/Android/发送 HTTP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43412921/