简体   繁体   English

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

[英]No HTTP request is send from retrofit /Android/

I'm trying to send data to an API from my Android project using Retrofit. 我正在尝试使用Retrofit从我的Android项目向API发送数据。 Everything seems to work without errors but no http request leaves the application. 一切似乎没有错误,但没有http请求离开应用程序。 I can confirm this with Wireshark screening and API console log. 我可以通过Wireshark筛选和API控制台日志来确认这一点。 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: 这里的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'

This only prepares the request, not sends it. 这只会准备请求,而不是发送请求。

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

Try making upload.enqueue() 尝试制作upload.enqueue()

你永远不会入队或执行调用,执行异步调用服务器使用upload.enqueue(new CallBack here)执行同步立即使用upload.execute()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM