0

I got stuck with an issue in iOS application using PhoneGap framework. I have a web services URL. I need to get JSON response from the web services URL. I had build up some code, but it is not working.

Here is my code:

<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!');
                      });

        };

How can I get the JSON response from the url?

4

2 に答える 2

0

.Net Webサービスを使用してWebサービスにアクセスしました。また、.NetWebサービスを呼び出すためのプラグインを作成しました。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(){}

/ ** * @parammethodnwebServiceが必要なメソッド名*@paramsuccessCallbackディレクトリリストが成功したときに呼び出されるコールバック*@paramfailureCallbackディレクトリリストがエラーを発生させたときに呼び出されるコールバック*/

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 モバイルで 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 に答える