【发布时间】:2015-05-28 13:07:09
【问题描述】:
我正在使用 .net Web 服务通过 ksoap 发送一个 json 字符串。我正在使用异步任务来运行 Web 服务,它在 3g、2g 和 wifi 上成功运行,但在运行时出现互联网连接问题,应用程序被强制关闭。 我的异步任务是-
public class AsyncCallSoapData extends AsyncTask<String, Void, String> {
private ProgressDialog dialog = new ProgressDialog(GPSActivity.this);
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(GPSActivity.this);
dialog.setMessage("Data sending...");
dialog.setIndeterminate(false);
dialog.setCancelable(false);
dialog.show();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
GPSWebserviceCall cs = new GPSWebserviceCall();
String response = cs.GetData();
return response;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
dialog.dismiss();
res = Integer.parseInt(result);
if (res > 0) {
Toast.makeText(GPSActivity.this, "Data successfully send", Toast.LENGTH_LONG).show();
}
}
}
网络服务类
package com.mycode.webservice;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import com.quantta.gps.GPSActivity;
public class GPSWebserviceCall {
public String GetData() {
String SOAP_ACTION = "http://tempuri.org/IService/dayRecord";
String OPERATION_NAME = "dayRecord";
String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
String SOAP_ADDRESS = "http://swift.quantta.com/Service.svc";
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME);
PropertyInfo PI = new PropertyInfo();
PI.setName("id");
PI.setValue(GPSActivity.header_name);
PI.setType(String.class);
request.addProperty(PI);
PI = new PropertyInfo();
PI.setName("JSONformat");
PI.setValue(GPSActivity.myJson);
PI.setType(String.class);
request.addProperty(PI);
SoapSerializationEnvelope envelop = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelop.dotNet = true;
envelop.setOutputSoapObject(request);
String strResponse = null;
try {
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
httpTransport.setXmlVersionTag("<?xml version='1.0' encoding='utf-8'?>");
httpTransport.debug = true;
httpTransport.call(SOAP_ACTION, envelop);
strResponse = httpTransport.responseDump;
String response = "";
SoapPrimitive result = (SoapPrimitive) envelop.getResponse();
if (result != null)
response = result.toString();
return response;
} catch (Exception e) {
strResponse = "Error :"+e ;
}
return strResponse;
}
}
如何在运行时实现,由于互联网连接,服务器没有响应,asynctask 应该停止,但应用程序不应该强制关闭。如果你能用我的代码给我实现,请帮助我。
【问题讨论】:
-
请发布错误
-
您在清单中添加了 Internet 权限?
-
是的,我在清单上添加了互联网权限
-
实际上它在 2g、3g 和 wifi 下工作得很好,但是当互联网运行时间太慢时,异步任务运行很长时间并且应用程序强制关闭,如果需要,是否需要使用 http 超时告诉如何......
标签: java android web-services android-asynctask