改造 /Android/ 没有发送 HTTP 请求

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();
    }

不确定我错过了什么。有什么想法吗?

P.S。这是应用程序在这部分使用的依赖项:

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()