【问题标题】:Android application force close calling ksoap web serviceAndroid应用强制关闭调用ksoap web服务
【发布时间】: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


【解决方案1】:

如果您的请求失败,则返回 null。因为如果发生任何网络中断,您已经妥善处理。

if (result != null)
{
  response = result.toString();
 }else{
      return null;
 }

或将response 初始化为null。从上面的代码中,您的初始化被观察为空字符串。

OnPostexecute() 中接收 null 时进行 null 检查。

  @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
       if(result!=null){
      }else{
      // Your message to user saying that user's internetconnection is lost.
        }
    }

【讨论】:

    【解决方案2】:

    您必须检查网络服务调用的互联网连接并管理您可以使用 try-catch 块管理的所有异常情况。

    【讨论】:

      【解决方案3】:

      在这种情况下,我正在使用模块“线程”来创建新的“线程”来发送 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 dev 的新手。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-14
        • 1970-01-01
        • 1970-01-01
        • 2016-12-20
        • 1970-01-01
        • 1970-01-01
        • 2015-09-07
        • 1970-01-01
        相关资源
        最近更新 更多