2

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?

P.S. 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'
nikitz
  • 1,051
  • 2
  • 12
  • 35

2 Answers2

2

This only prepares the request, not sends it.

Call<Void> upload = api.newObj(obj);

Try making upload.enqueue()

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Thank you, this solved it. The retrofit official examples were not detailed enough. When you mentioned enqueue I found the original documentation. Here is a link for someone else if needed: https://square.github.io/retrofit/2.x/retrofit/retrofit2/Call.html#enqueue-retrofit2.Callback- – nikitz Apr 14 '17 at 14:16
  • This is the actual documentation. https://github.com/square/retrofit/wiki/Retrofit-Tutorials – OneCricketeer Apr 14 '17 at 16:41
1

You never enqueue or execute the call, to perform asynchronous call to server use upload.enqueue(new CallBack here) to perform synchronous immediately use upload.execute()

Remario
  • 3,813
  • 2
  • 18
  • 25