duanbaque4230 2012-04-18 12:59
浏览 98
已采纳

Android KSOAP2请求PHP SOAP服务器

I am trying to create a SOAP client in my android app. I have already tested the server with php SOAP client, and it has been working. But in my android app I am still getting exceptions. Can anyone help me whats wrong and how to fix it? Thanks

PHP

<?php
class service
{      
public function service()
{   }

public function login($nickname, $password)
{   

    $sql = "select nick from user 
    where 
    nick =\"$nick\"
    and
    pass = \"$password\"";

    if (($result = $this->db->query($sql)) && ($result->num_rows == 1))
        return true;
    else
        return false;
}  
}

$server = new SoapServer(null, array(
'uri' => "urn://www.domain.cz",
'soap_version' => SOAP_1_2)
);              
$server->setClass("service");     
$server->handle(); 
?>

Android

private static String SOAP_ACTION = "http://www.domain.cz/server/server.php";
    private static String NAMESPACE = "urn://www.domain.cz";
    //need fix namespace to work, final solution
    // private static String NAMESPACE = "http://www.domain.cz/server/";
    private static String METHOD_NAME = "login";
    private static String URL = "http://www.domain.cz";

public void Connect()
{
    //Initialize soap request + add parameters
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    //Use this to add parameters
    request.addProperty("nick","peter");
    request.addProperty("password","somepassword");

    //Declare the version of the SOAP request
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
    envelope.setOutputSoapObject(request);

    //Needed to make the internet call
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    try {
            //this is the actual part that will call the webservice
            androidHttpTransport.call(SOAP_ACTION, envelope);//HERE is xmlpullparserexception after a while
    } catch (Exception e) {
            e.printStackTrace();
    }

    // Get the SoapResult from the envelope body.
    SoapObject result = (SoapObject)envelope.bodyIn;

    if(result != null){
            TextView t = (TextView)this.findViewById(R.id.resultbox);

            //Get the first property and change the label text
            t.setText("SOAP response:

" + result.getProperty(0).toString());
    }

}
  • 写回答

2条回答 默认 最新

  • douyun1950 2012-04-18 18:41
    关注

    Your URL should be the soap endpoint, i.e. http://www.domain.cz/server/server.php

    Your SOAP_ACTION should contain the method name, i.e. http://www.domain.cz/server/server.php/login

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?