我正在使用.net web服务使用ksoap发送json字符串。我正在使用异步任务来运行Web服务,它在3g,2g和wifi上成功运行,但在运行时互联网连接问题,应用程序强制关闭。 我的asynctask是 -
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();
}
}
}
Web服务类
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应该停止,但是应用程序不应该强制关闭。如果你能用我的代码给我实现,请帮助我。
答案 0 :(得分:0)
如果您的请求失败,则返回null。因为如果发生任何网络中断,您可以正确处理它。
if (result != null)
{
response = result.toString();
}else{
return null;
}
或将response
初始化为null
。从上面的代码中,您初始化被视为空字符串。
On OnPostexecute()
中的接收空值有空检查。
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(result!=null){
}else{
// Your message to user saying that user's internetconnection is lost.
}
}
答案 1 :(得分:0)
您必须检查Web服务调用上的Internet连接并管理可以使用try-catch块管理的所有异常情况。
答案 2 :(得分:0)
在这种情况下,我正在使用模块“线程”创建新的“线程”以发送Web服务,例如
public int onStartCommand(Intent intent, int flags, int startId){
Thread t = new Thread(){
public void run(){
try {
boolean testConnection = SendData();
} catch (Exception exception) {
exception.printStackTrace();
}
}
};
t.start();
return flags;
}
调用功能
public void SendData() {
String TAG = "test connection";
String NAMESPACE = "http://tempuri.org/mywebservice/Service1";
String SOAP_ACTION = "http://tempuri.org/mywebservice/Service1/HelloWorld";
String METHOD_NAME = "HelloWorld";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(path, 30000);
Object response = null;
//Enable Strict mode for policy android
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
httpTransport.call(SOAP_ACTION, envelope);
Log.v(TAG, "out :" + envelope.bodyOut.toString());
Log.v(TAG, "in :" + envelope.bodyIn.toString());
httpTransport.getServiceConnection().disconnect();
//return something
return true;
} catch (Exception exception) {
exception.printStackTrace();
//return something
return false;
}
}
如果您使用这样的代码,则您的应用不会被Android强制关闭。
抱歉,代码中的内容不好,因为android开发是新的。