我想要创建一个简单的应用程序,当按钮clicks.The应用工作流像这样时,它打算在POST方法中调用API;用户输入url和一个超时值,然后按start按钮。API将在所提供的url中调用,并将根据所提供的超时值一次又一次地调用。当用户按下“停止”按钮时,所有活动如果stop.The调用都不会提供任何响应数据。我只想按POST method.In顺序调用那个API,为了达到这个目的,我在一个服务中编写了API调用。
我面临的问题是
2.如何根据所提供的超时值在后台调用此api。
我做了什么,
My MainActivity
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服务类
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帮助是实用的
发布于 2019-05-13 09:27:19
https://stackoverflow.com/questions/56109228
复制相似问题