展开全部
你应该写清楚应用场景,懒得写代码,只写思路
分两种使用场景吧,第一种,就是无脑post,对服务器造成很大负担(如果这就是你的目的),那你只需要写个timer循环,每隔一秒或者几秒post服务端就行了
第二种,在get到结果后循环post,这个方式比较合理,不需要timer循环,对服务器负荷也小,需要一个hanlderMessage,在get到结果或异常后给hanlderMessage发送消息,handlerMessage再执行一次post即可
分两种使用场景吧,第一种,就是无脑post,对服务器造成很大负担(如果这就是你的目的),那你只需要写个timer循环,每隔一秒或者几秒post服务端就行了
第二种,在get到结果后循环post,这个方式比较合理,不需要timer循环,对服务器负荷也小,需要一个hanlderMessage,在get到结果或异常后给hanlderMessage发送消息,handlerMessage再执行一次post即可
展开全部
String rs="";
try {
final String SERVER_URL = "http://127.0.0.1/service.asmx";
String nameSpace = "http://tempuri.org/";//命名空间
String methodName = "HelloWorld";//方法名
String soapAction = "http://tempuri.org/HelloWorld";//HelloWorld 命名空间/方法名
//创建SoapObject实例
SoapObject request = new SoapObject(nameSpace, methodName);
request.addProperty("name",gpp);
request.addProperty("jd",gpp2);
rs="到这里了";
//生成调用web service方法的soap请求消息
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true; //设置.net web service
envelope.setOutputSoapObject(request);//发送请求
rs="到这里了2";
HttpTransportSE androidHttpTransport = new HttpTransportSE(SERVER_URL);
rs="到这里了3";
androidHttpTransport.call(soapAction, envelope);
rs="到这里了4";
Object result = (Object) envelope.getResponse();
//textView.setText(e.getMessage());
return result.toString();
//textView.setText("7");
// new AlertDialog.Builder(this).setTitle("Hint").setMessage(result.toString()).setPositiveButton("OK", null).show();
}
catch (Exception e)
{
System.out.println(e.getMessage());
// textView.setText(e.getMessage());
// new AlertDialog.Builder(this).setTitle("Hint").setMessage(e.getMessage()).setPositiveButton("OK", null).show();
}
return rs;
}
@Override
protected void onProgressUpdate(Integer... progress) {
//这个函数在doInBackground调用publishProgress时触发,虽然调用时只有一个参数
//但是这里取到的是一个数组,所以要用progesss[0]来取值
//第n个参数就用progress[n]来取值
tv.setText(progress[0]+"%");
super.onProgressUpdate(progress);
}
@Override
protected void onPostExecute(String result) {
//doInBackground返回时触发,换句话说,就是doInBackground执行完后触发
//这里的result就是上面doInBackground执行后的返回值,所以这里是"执行完毕"
setTitle(result);
tv.setText(result);
super.onPostExecute(result);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
try {
final String SERVER_URL = "http://127.0.0.1/service.asmx";
String nameSpace = "http://tempuri.org/";//命名空间
String methodName = "HelloWorld";//方法名
String soapAction = "http://tempuri.org/HelloWorld";//HelloWorld 命名空间/方法名
//创建SoapObject实例
SoapObject request = new SoapObject(nameSpace, methodName);
request.addProperty("name",gpp);
request.addProperty("jd",gpp2);
rs="到这里了";
//生成调用web service方法的soap请求消息
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true; //设置.net web service
envelope.setOutputSoapObject(request);//发送请求
rs="到这里了2";
HttpTransportSE androidHttpTransport = new HttpTransportSE(SERVER_URL);
rs="到这里了3";
androidHttpTransport.call(soapAction, envelope);
rs="到这里了4";
Object result = (Object) envelope.getResponse();
//textView.setText(e.getMessage());
return result.toString();
//textView.setText("7");
// new AlertDialog.Builder(this).setTitle("Hint").setMessage(result.toString()).setPositiveButton("OK", null).show();
}
catch (Exception e)
{
System.out.println(e.getMessage());
// textView.setText(e.getMessage());
// new AlertDialog.Builder(this).setTitle("Hint").setMessage(e.getMessage()).setPositiveButton("OK", null).show();
}
return rs;
}
@Override
protected void onProgressUpdate(Integer... progress) {
//这个函数在doInBackground调用publishProgress时触发,虽然调用时只有一个参数
//但是这里取到的是一个数组,所以要用progesss[0]来取值
//第n个参数就用progress[n]来取值
tv.setText(progress[0]+"%");
super.onProgressUpdate(progress);
}
@Override
protected void onPostExecute(String result) {
//doInBackground返回时触发,换句话说,就是doInBackground执行完后触发
//这里的result就是上面doInBackground执行后的返回值,所以这里是"执行完毕"
setTitle(result);
tv.setText(result);
super.onPostExecute(result);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2019-11-12
展开全部
handler+Runnable 做定时任务
public class MainActivity extends AppCompatActivity {
private static boolean flag = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTimer();
}
private void setTimer(){
mHandler.postDelayed(runnable, 1000);
}
private Handler mHandler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
//在这里执行定时需要的操作
if (flag) {
mHandler.postDelayed(this, 1000);
}
}
};
private void stopTimer(){
flag = false;
}}
public class MainActivity extends AppCompatActivity {
private static boolean flag = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTimer();
}
private void setTimer(){
mHandler.postDelayed(runnable, 1000);
}
private Handler mHandler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
//在这里执行定时需要的操作
if (flag) {
mHandler.postDelayed(this, 1000);
}
}
};
private void stopTimer(){
flag = false;
}}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询