0

我在使用 PhoneGap 框架的 iOS 应用程序中遇到了一个问题。我有一个网络服务 URL。我需要从 Web 服务 URL 获取 JSON 响应。我已经建立了一些代码,但它不起作用。

这是我的代码:

<div data-role="content" data-theme="a" style="background: Black">
    <div data-theme="a">
        <span style="font-size: x-large; color: Orange;">Secure Log In</span></div>
    <div data-theme="a">
        <div data-theme="a">
            <input type="password" placeholder="PASSWORD" id="txtPassword" style="background-color: gray;" /></div>
     <div data-theme="a" align="right">
        <a href="#" data-role="button" onclick="callWebService()" data-corners="false"
                data-theme="a" id="clcik" cursor="pointer" style="width: 150px; border-radius: 5px 5px 5px 5px"
                data-clickload="show" data-transition="slidefade"><span style="color: Green">Log In</span>
            </a>
        </div>

        function callWebService(){

            var query = 'Ocean';
            var url = 'http://66.171.142.16/Accountservice/Security/ValidateAccess?accesscode=abcd&type=1';
            alert(url);

            $.getJSON(url,function(response){
                      alert('Here!');
                      });

        };

如何从 url 获取 JSON 响应?

4

2 回答 2

0

我使用 .Net Web Service 来访问 Web Service,还创建了一个插件来调用 .Net Web Service。在 Java 脚本中,我曾经调用 Web 服务方法,如下所述。

在 script.js 中

   $(".CategoryNavbar").click(function(e){
                     e.preventDefault();
                      window.plugins.webservice.GetFlights("service",function(r){printResult(r)},function(e){console.log(e)});
                     return false;  
                 });


  function printResult(fileInfo){


                var innerHtmlText=getHtml(fileInfo);  

                  $.mobile.changePage('#CategoryPage',{transition:'slide'});

                    $('#CategoryPageContent').html(innerHtmlText);

                    $("#CategoryList").listview();
                    $("#CategoryList").listview('refresh');



            }




  function getHtml(fileInfo){
                 var htmlText='<ul data-role="listview" id="CategoryList" data-theme="c" data-filter="true" data-filter-placeholder="Search">';

                 for(var index=0;index<fileInfo.Flights.length;index++){
                     htmlText=htmlText+'<li> <a href="">'+ fileInfo.Flights[index] +'</a></li>';
                     }

                 htmlText=htmlText+"</ul>";
                 return htmlText;

                 } 

在插件文件中 /** * 构造函数 */ function WebService() { }

/** * @param methodn 我们想要的 webService 的方法名称 * @param successCallback 目录列出成功时调用的回调 * @param failureCallback 目录列出遇到错误时调用的回调 */

WebService.prototype.GetFlights = function(args, successCallback,
        failureCallback) {
    return cordova.exec(successCallback, failureCallback, 'WebService',
            'GetFlights', [ args ]);
};

if (!window.plugins) {
    window.plugins = {};
}
if (!window.plugins.webservice) {
    window.plugins.webservice = new WebService();
}
于 2013-02-27T08:43:49.387 回答
0

嗨 Sudheer 请检查以下代码以使用 Ksoap 从 Web 服务获取响应

public class AndroidWebService extends Activity {

private final String NAMESPACE = "http://www.webserviceX.NET/";
private final String URL = "http://www.webservicex.net/ConvertWeight.asmx";
private final String SOAP_ACTION = "http://www.webserviceX.NET/ConvertWeight";
private final String METHOD_NAME = "ConvertWeight";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

    String weight = "3700";
    String fromUnit = "Grams";
    String toUnit = "Kilograms";

    PropertyInfo weightProp =new PropertyInfo();
    weightProp.setName("Weight");
    weightProp.setValue(weight);
    weightProp.setType(double.class);
    request.addProperty(weightProp);

    PropertyInfo fromProp =new PropertyInfo();
    fromProp.setName("FromUnit");
    fromProp.setValue(fromUnit);
    fromProp.setType(String.class);
    request.addProperty(fromProp);

    PropertyInfo toProp =new PropertyInfo();
    toProp.setName("ToUnit");
    toProp.setValue(toUnit);
    toProp.setType(String.class);
    request.addProperty(toProp);

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

    try {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
        Log.i("myApp", response.toString());

        TextView tv = new TextView(this);
        tv.setText(weight+" "+fromUnit+" equal "+response.toString()+ " "+toUnit);
        setContentView(tv);

    } catch (Exception e) {
        e.printStackTrace();
    }
         }
     }

这是在 jquery mobile 中使用 JSON 获取响应的示例代码,请查看下面的代码

$.ajax({
    cache: false,
    url: wcfServiceUrl + "Authenticate?CompanyID=" + escape(comp) + "&UserName=" + user + "&Password=" + escape(pass) + "&Auth=" + ipaddress+"",
    data: "{}",
    type: "GET",
    contentType: "application/javascript",
    dataType: "jsonp",
    beforeSend: function (XMLHttpRequest) {
        $.mobile.loadingMessageTextVisible = true;
        $.mobile.loadingMessage = "Loading Please Wait";
        $.mobile.showPageLoadingMsg();
    }, 
    complete: function (XMLHttpRequest, textStatus) {
        $.mobile.hidePageLoadingMsg();
    },
    error: function (xmlHttpRequest, status, err) {
                $.mobile.loadingMessageTextVisible = true;
                $.mobile.loadingMessage = "Web service is not responding. Try again";
                $.mobile.showPageLoadingMsg();
                var wait = setTimeout(function(){
                    $.mobile.hidePageLoadingMsg();
                },400);
    },
    error: function () {
        jAlert("list failed!",alertmessage);
    },
    success: function (list) {
        var rar = list.split(";");
        if(rar[0]=="Error")
        {
            jAlert(rar[1],alertmessage);
        }
        else if(rar[0]=="Success")
        {
            localStorage.setItem( "CompanyID", comp);
            localStorage.setItem( "Username", user);
            localStorage.setItem( "UserID", rar[1]);
            $.mobile.changePage( '#home', { transition: "pop", reverse: false } );
        }
        else if(rar[0]=="FirstLogin")
        {
            localStorage.setItem( "CompanyID", comp);
            localStorage.setItem( "Username", user);
            localStorage.setItem( "UserID", rar[1]);
            $.mobile.changePage( '#change-pass', { transition: "slide", reverse: false } );
        }
    }
});
于 2013-02-27T09:03:03.533 回答