调用 Web 服务时 Android 代码不返回 String

发布于 2024-11-27 09:16:13 字数 3027 浏览 1 评论 0原文

我在从 Android 代码调用 Web 服务时遇到问题。我的 Web 服务代码 返回一个字符串,中间有分隔符“#”。现在我需要在 Android 中编写一个代码来调用 Web 服务,分隔分隔符,以便使用复选框显示实际名称,供用户选择

以下是我的 Android 代码:

package com.app;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.TextView;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class TestApp extends Activity {
private static final String SOAP_ACTION = "http://tempuri.org/getData";




  private static final String METHOD_NAME = "getData";


    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://10.0.2.2/login2/Service1.asmx";
    TextView tv;

    boolean[] bln1=null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv=(TextView)findViewById(R.id.text1);

        call();

        String[] arr={""};
        boolean[] bln = {false, false, false};

       bln1 = new boolean[arr.length];


        new AlertDialog.Builder(TestApp.this)
        .setIcon(R.drawable.alert_dialog_icon)
        .setTitle("Title")
        .setMultiChoiceItems(arr,
                bln,
                new DialogInterface.OnMultiChoiceClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton,
                            boolean isChecked) {


                        if(isChecked){
                            bln1[whichButton] = true;
                        }
                        else{
                            bln1[whichButton] = false;
                        }
                    }
                })
        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {


            }
        })
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {


            }
        })
       .show();


    }




    public void call()
    {
        try {

            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

            //request.addProperty("passonString", "Hello Android");



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

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            androidHttpTransport.call(SOAP_ACTION, envelope);

            Object result = (Object)envelope.getResponse();


            tv.setText(result.toString());
        } catch (Exception e) {
            tv.setText(e.getMessage());
            }
    }
}

I am facing a problem calling the web service from my Android code. My web service code returns a string with a delimiter '#' in between. Now i need to make a code in Android that will call the web service , separate the delimiters such that the actual names are displayed using a check box for the user to select

Here is my Android code :

package com.app;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.TextView;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class TestApp extends Activity {
private static final String SOAP_ACTION = "http://tempuri.org/getData";




  private static final String METHOD_NAME = "getData";


    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://10.0.2.2/login2/Service1.asmx";
    TextView tv;

    boolean[] bln1=null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv=(TextView)findViewById(R.id.text1);

        call();

        String[] arr={""};
        boolean[] bln = {false, false, false};

       bln1 = new boolean[arr.length];


        new AlertDialog.Builder(TestApp.this)
        .setIcon(R.drawable.alert_dialog_icon)
        .setTitle("Title")
        .setMultiChoiceItems(arr,
                bln,
                new DialogInterface.OnMultiChoiceClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton,
                            boolean isChecked) {


                        if(isChecked){
                            bln1[whichButton] = true;
                        }
                        else{
                            bln1[whichButton] = false;
                        }
                    }
                })
        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {


            }
        })
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {


            }
        })
       .show();


    }




    public void call()
    {
        try {

            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

            //request.addProperty("passonString", "Hello Android");



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

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            androidHttpTransport.call(SOAP_ACTION, envelope);

            Object result = (Object)envelope.getResponse();


            tv.setText(result.toString());
        } catch (Exception e) {
            tv.setText(e.getMessage());
            }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷雾森÷林ヴ 2024-12-04 09:16:13

而不是将大小写转换为对象 Object result = (Object)envelope.getResponse(); 转换

  SoapPrimitive responsesData = (SoapPrimitive) envelopes.getResponse();
  String result = responsesData .toString();

insteand of casing to object Object result = (Object)envelope.getResponse(); casting

  SoapPrimitive responsesData = (SoapPrimitive) envelopes.getResponse();
  String result = responsesData .toString();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文