[英]How to get JSON response from the web services URL in PhoneGap?
我在使用 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 響應?
我使用 .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();
}
嗨 Sudheer 請檢查以下代碼以使用 Ksoap 從網絡服務獲取響應
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 } );
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.