首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在后台执行API调用?

如何在后台执行API调用?
EN

Stack Overflow用户
提问于 2019-05-13 09:15:54
回答 1查看 8.5K关注 0票数 1

我想要创建一个简单的应用程序,当按钮clicks.The应用工作流像这样时,它打算在POST方法中调用API;用户输入url和一个超时值,然后按start按钮。API将在所提供的url中调用,并将根据所提供的超时值一次又一次地调用。当用户按下“停止”按钮时,所有活动如果stop.The调用都不会提供任何响应数据。我只想按POST method.In顺序调用那个API,为了达到这个目的,我在一个服务中编写了API调用。

我面临的问题是

  1. POST方法不起作用。

2.如何根据所提供的超时值在后台调用此api。

我做了什么,

My MainActivity

代码语言:javascript
复制
  url   = (EditText)findViewById(R.id.editText3);
        timeOut   = (EditText)findViewById(R.id.editText5);
        Button clickButton = (Button) findViewById(R.id.start);
        clickButton.setOnClickListener( new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent intent = new Intent(MainActivity.this, APIService.class);
                Bundle bund = new Bundle();

                bund.putString("url",url.getText().toString());
                bund.putString("timeout",timeOut.getText().toString());
                intent.putExtras(bund);
                startService(intent);


            }
        });

        Button stopbutton = (Button) findViewById(R.id.stop);
        stopbutton.setOnClickListener( new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub


                Intent intent = new Intent(MainActivity.this, APIService.class);
                stopService(intent);

            }
        });

我的API服务类

代码语言:javascript
复制
public APIService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        Toast.makeText(this, " Client API Service Started", Toast.LENGTH_LONG).show();
        super.onCreate();
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Client API Service Started", Toast.LENGTH_LONG).show();
        String WEBURL;
        String TimeOut;
        Bundle bund = intent.getExtras();
        WEBURL=bund.getString("url");
        TimeOut=bund.getString("timeout");
        String URL= "http://"+WEBURL+"/API/ReportSubscription/GetAllReportSubscription?subscriptionUR="+WEBURL;
        new HttpRequestTask(
                new HttpRequest(URL, HttpRequest.POST),
                new HttpRequest.Handler() {
                    @Override
                    public void response(HttpResponse response) {
                        if (response.code == 200) {
                            Log.d(this.getClass().toString(), "Request successful!");
                        } else {
                            Log.e(this.getClass().toString(), "Request unsuccessful: " + response);
                        }
                    }
                }).execute();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Client API Service Stopped", Toast.LENGTH_LONG).show();
    }
}

我使用编译'com.apptakk.http_request:http-request:0.1.2' for Web call.Any帮助是实用的

EN

回答 1

Stack Overflow用户

发布于 2019-05-13 09:27:19

  1. 首先,通过Postman进行测试,确保您的API工作正常。在确保API从后端本身运行良好之后,问题在于您的移动端调用该API。我建议改进(https://square.github.io/retrofit/)异步地尝试API调用任务,因为它非常简单和标准,.You在api中使用POST方法,将超时值作为参数传递,api url作为基url。
  2. 您可以使用android中的服务在后台执行,即API调用
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56109228

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档