I'm trying to send data to an API from my Android project using Retrofit. Everything seems to work without errors but no http request leaves the application. I can confirm this with Wireshark screening and API console log. Here is an example pseudo code of this parts of my application:
// 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();
}
Not sure what I'm missing. Any ideas?
PS here are the dependencies used by the app for this part:
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'
This only prepares the request, not sends it.
Call<Void> upload = api.newObj(obj);
Try making upload.enqueue()
你永远不会入队或执行调用,执行异步调用服务器使用upload.enqueue(new CallBack here)
执行同步立即使用upload.execute()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.