【问题标题】:callig C# web service in android在android中调用C# Web服务
【发布时间】:2016-03-10 07:10:28
【问题描述】:

我想在 android 中调用 asp.net 网络服务。 (那个网络服务很简单,把两个整数相加并返回结果。)

我的问题是:当我运行我的应用程序并单击按钮时(用于从文本字段中提供数字并通过 Web 服务求和并通过 AlertDialog 显示结果) 应用程序被冻结在一种状态,因为它不起作用。这是我在该状态下的应用截图(点击按钮后): freeze in this status! :-(

在很长一段时间后显示这个按摩: “Soap 没有响应。关闭它是什么?”。

请帮帮我!

这在我的网络服务代码中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

// [System.Web.Script.Services.ScriptService]

public class Service : System.Web.Services.WebService
{
public Service () {

    //Uncomment the following line if using designed components 
    //InitializeComponent();  
}

[WebMethod]
public int add (int a, int b) {
    return a + b;

}

}

这是CallSoap,这个类调用我的网络服务:

import org.ksoap2.SoapEnvelope; 
import org.ksoap2.serialization.PropertyInfo; 
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class CallSoap  
{
public final String SOAP_ACTION = "http://tempuri.org/add";

public  final String OPERATION_NAME = "add"; 

public  final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";

public  final String SOAP_ADDRESS = "http://s1.azarindk.co.ir/service.asmx";
public CallSoap() 
{ 
}
public String Call(int a,int b)
{
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
//PropertyInfo pi =new PropertyInfo();

request.addProperty("a", Integer.valueOf(a));
request.addProperty("b", Integer.valueOf(b));

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;

envelope.setOutputSoapObject(request);

HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
Object response=null;
try
{
httpTransport.call(SOAP_ACTION, envelope);
response = envelope.getResponse();
}
catch (Exception exception)
{
response=exception.toString();
}
System.out.print(response.toString());
return response.toString();


}
}

这是一个在主Activity中调用CallSoap的线程:

package com.example.soap;
public class Caller  extends Thread  
{
public CallSoap cs;
public int a,b; 


public void run(){
    try{
        cs=new CallSoap();
        String resp=cs.Call(a, b);
        MainActivity.rslt=resp;
    }catch(Exception ex)
    {MainActivity.rslt=ex.toString();
    }    
}
}

这是我的主要活动:

package com.example.soap;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

public static String rslt="";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button b1=(Button)findViewById(R.id.button1);
    final  AlertDialog ad=new AlertDialog.Builder(this).create();

    b1.setOnClickListener(new OnClickListener() { 

        @Override
        public void onClick(View arg0) {
        // TODO Auto-generated method stub

        CallSoap cs=new CallSoap();

         try
         { 
             EditText ed1=(EditText)findViewById(R.id.editText1);
             EditText ed2=(EditText)findViewById(R.id.editText2); 
             int a=Integer.parseInt(ed1.getText().toString());
             int b=Integer.parseInt(ed2.getText().toString());
             rslt="START"; 

             Caller c=new Caller();
             c.a=a;
             c.b=b; 
        // c.ad=ad;

             c.join();
             c.start();

             while(rslt=="START") {
                 try {
                     Thread.sleep(10); 
                 }catch(Exception ex) {
                 }
             }

             ad.setTitle("RESULT OF ADD of "+a+" and "+b);
             ad.setMessage(rslt); 

         }catch(Exception ex) {
             ad.setTitle("Error!"); ad.setMessage(ex.toString());
         }
         ad.show(); 
     } });

            }

 }

【问题讨论】:

    标签: java android web-services android-webservice


    【解决方案1】:

    您的主线程上有一个轮询循环,这非常糟糕。这并不比用网络事务本身阻塞主线程更好。此外,您的循环似乎永远不会终止,因为 rslt 的值永远不会改变。最后,不要使用 == 在 Java 中比较字符串。

    阅读在 Android 中处理线程工作的技术。你有很多选择,但你需要遵循一个好的模式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-04
      • 2021-01-06
      • 2014-08-23
      相关资源
      最近更新 更多