❶ IOS請求webservice,webservice是C#的,如何返回JSON
webservice這個好像不直接支持json的,一個比較好的替代方法是在C#端先把返回值序列化成json字元串在返回了,
但是這可能會限製程序的易用性和伸縮性,不過要是僅僅簡單的返回數據的話,應該沒有任何問題,這個可能需要你定義一些簡單的協議
❷ webservice,android訪問沒問題,ios訪問報錯、、、webservice服務端是用Java實現的 錯誤如下:
你能抓到發送到服務端的SOAP報文么?
這個異常通常都是SOAP報文內容有問題導致的
可能是報文字元串編碼不對也可能是報文的格式結構不對導致的服務端無法解析
❸ IOS 開發 webservice soap的請求
1. 改成同步
2. 用代碼控制當非同步執行後得到結果後再執行 下面的代碼
❹ ios用webservice介面傳參數出現: 意外的元素
很明顯了,人家需要的是 arg1 和 arg0, 你給了userName, 人家當然報錯了,
是不是服務端 沒有對變數 標識 @webParam(name="userName")
❺ 在ios中webseriver還在用嗎
1.開啟Xcode創建一個項目,項目類型選擇Single View Application。
2.創建三個Group,並導入上述三個庫。
JSON:將JSON\Classes目錄的文件托入剛才創建的JSON GROUP。
ASIHTTPRequest:將ASIHTTPRequest\Classes目錄的所有文件拖入創建的ASIHTTPRequest GROUP(注意,只要當前目錄的文件,CloudFiles之類的目錄不需要)
ASIHTTPRequest\External\Reachability這里的文件也要加進來
MBProgressHUD:將MBProgressHUD.m和MBProgressHUD.h拖入MBProgressHUD GROUP
以上三個操作,拖入的時候,記得勾選Copy items into destination group』s folder (if needed)選項,意思是把目錄復制到你的項目中,而不是只引用。
3.導入一些必要的frameworks:點擊左側導航欄中你的項目->選中target->再選擇build phases欄0->Link Binary with Libraries。點擊+按鈕,搜索CFNetwork.framework and SystemConfiguration.framework,MobileCoreServices.framework, and libz.1.2.3.dylib四個庫。
以上三個大步驟完成後,點擊編譯。完成第一個階段。
二、實現Interface
創建UI: 1.label
2.textfield
3.textview
三、與WebService交互
我們的Web Service需要三個參數:
rw_app_id: 應用的唯一標識號. If you』ve been following along with the previous tutorial, there should be only one entry so far, App ID #1.
code: The code to attempt to redeem. This should be a string that』s entered by the user.
device_id: The device ID that is attempting to redeem this code. We can get this with an easy API call
我們需要使用POST機制請求WebService。ASIHTTPRequest將使這一過程變得很便捷。
1.創建一個ASIFormDataRequest實例與URL
2.使用setPostValue方法指定各個參數
3.設置viewcontroller為request的delegate,之後調用startAsynchronous來發起非同步請求
4.當請求完畢後,requestFinished或者requestFailed會被回調
5.requestFinished無論webservice相應一個錯誤的代碼,或者正確響應,都會被調用,所以在這個函數里要檢查請求成功或者失敗
6.如果一切順利,再解析收到的JSON數據
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(@"Want to redeem: %@", textField.text);
// Get device unique ID
UIDevice *device = [UIDevice currentDevice];
NSString *uniqueId= [device uniqueIdentifier];
// Start request
NSString *code = textField.text;
NSURL *url = [NSURL URLWithString:@"http://www.wildfables.com/promos/"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:@"1" forKey:@"rw_app_id"];
[request setPostValue:code forKey:@"code"];
[request setPostValue:uniqueId forKey:@"device_id"];
[request setDelegate:self];
[request startAsynchronous];
// Hide keyword
[textField resignFirstResponder];
// Clear text field
textView.text = @"";
//狀態指示器
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.labelText = @"Redeeming code...";
return TRUE;
}
/*請求完成後回調*/
- (void)requestFinished:(ASIHTTPRequest *)request
{
[MBProgressHUD hideHUDForView:self.view animated:YES];
if (request.responseStatusCode == 400) {
textView.text = @"Invalid code";
} else if (request.responseStatusCode == 403) {
textView.text = @"Code already used";
} else if (request.responseStatusCode == 200) {
NSString *responseString = [request responseString];
NSDictionary *responseDict = [responseString JSONValue];
NSString *unlockCode = [responseDict objectForKey:@"unlock_code"];
if ([unlockCode compare:@"com.razeware.test.unlock.cake"] == NSOrderedSame) {
textView.text = @"The cake is a lie!";
} else {
textView.text = [NSString stringWithFormat:@"Received unexpected unlock code: %@", unlockCode];
}
} else {
textView.text = @"Unexpected error";
}
}
/*請求失敗後回調*/
- (void)requestFailed:(ASIHTTPRequest *)request
{
[MBProgressHUD hideHUDForView:self.view animated:YES];
NSError *error = [request error];
textView.text = error.localizedDescription;
}
為了讓用戶感受到,在請求數據的時候,程序在運行,而不是假死,所以要添加狀態指示器。
三個步驟
// Add at the top of the file#import "MBProgressHUD.h"
// Add right before return TRUE in textFieldShouldReturn
MBProgressHUD *hud =[MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.labelText =@"Redeeming code...";
// Add at start of requestFinished AND requestFailed
[MBProgressHUD hideHUDForView:self.view animated:YES];
編譯運行,大功告成。
❻ ios,webservice傳過來的xml數據用自帶的xmlparser解析,怎麼讓它成list樣式
通過web服務傳遞的數據的序列號為字元串或位元組,建議字元串,除非圖片等可以考慮位元組流。 傳統的json或xml都可以的,直接傳遞list得轉換下才可以。
❼ iOS請求webservice介面,參數類型是buffer流,怎麼破
數據流在ios客戶端向伺服器端提交數據時使用的類型可以用NSData. 這需要你將客戶端要提交的數據先轉成NSData類型。如我們在ios客戶端向伺服器端上傳圖片時,就需要將UIImage對象轉成NSData並提交到伺服器端。
❽ 如何用IOS調用WebService
ava調用WebService可以直接使用Apache提供的axis.jar自己編寫代碼,或者利用Eclipse自動生成WebService Client代碼,利用其中的Proxy類進行調用。理論上是一樣的,只不過用Eclipse自動生成代碼省事些。
1、編寫代碼方式:
package com.yun.test;
import java.rmi.RemoteException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.message.PrefixedQName;
import org.apache.axis.message.SOAPHeaderElement;
import com.cezanne.golden.user.Exception;
import com.cezanne.golden.user.UserManagerServiceProxy;
import javax.xml.namespace.QName;
import java.net.MalformedURLException;
import javax.xml.rpc.ServiceException;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPException;
public class testWebService {
public static String getResult() throws ServiceException, MalformedURLException, RemoteException, SOAPException
{
//標識Web Service的具體路徑
String endpoint = "WebService服務地址";
// 創建 Service實例
Service service = new Service();
// 通過Service實例創建Call的實例
Call call = (Call) service.createCall();
//將Web Service的服務路徑加入到call實例之中.
call.setTargetEndpointAddress( new java.net.URL(endpoint) );//為Call設置服務的位置
// 由於需要認證,故需要設置調用的SOAP頭信息。
Name headerName = new PrefixedQName( new QName("發布的wsdl里的targetNamespace里的url", "string_itemName") );
org.apache.axis.message.SOAPHeaderElement header = new SOAPHeaderElement(headerName);
header.addTextNode( "blablabla" );
call.addHeader(header);
// SOAPHeaderElement soapHeaderElement = new SOAPHeaderElement("發布的wsdl里的targetNamespace里的url", "SoapHeader");
// soapHeaderElement.setNamespaceURI("發布的wsdl里的targetNamespace里的url");
// try
// {
// soapHeaderElement.addChildElement("string_itemName").setValue("blablabla");
// }
// catch (SOAPException e)
// {
// e.printStackTrace();
// }
// call.addHeader(soapHeaderElement);
//調用Web Service的方法
org.apache.axis.description.OperationDesc oper;
org.apache.axis.description.ParameterDesc param;
oper = new org.apache.axis.description.OperationDesc();
oper.setName("opName");
param = new org.apache.axis.description.ParameterDesc(new javax.xml.namespace.QName("", "arg0"), org.apache.axis.description.ParameterDesc.IN, new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"), java.lang.String.class, false, false);
param.setOmittable(true);
oper.addParameter(param);
param = new org.apache.axis.description.ParameterDesc(new javax.xml.namespace.QName("", "arg1"), org.apache.axis.description.ParameterDesc.IN, new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"), java.lang.String.class, false, false);
param.setOmittable(true);
oper.addParameter(param);
param = new org.apache.axis.description.ParameterDesc(new javax.xml.namespace.QName("", "arg2"), org.apache.axis.description.ParameterDesc.IN, new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"), java.lang.String.class, false, false);
param.setOmittable(true);
oper.addParameter(param);
oper.setReturnType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"));
oper.setReturnClass(java.lang.String.class);
oper.setReturnQName(new javax.xml.namespace.QName("", "return"));
oper.setStyle(org.apache.axis.constants.Style.WRAPPED);
oper.setUse(org.apache.axis.constants.Use.LITERAL);
oper.addFault(new org.apache.axis.description.FaultDesc(
new javax.xml.namespace.QName("發布的wsdl里的targetNamespace里的url", "Exception"),
"Exception",
new javax.xml.namespace.QName("發布的wsdl里的targetNamespace里的url", "Exception"),
true
));
call.setOperation( oper );
call.setOperationName(new javax.xml.namespace.QName("發布的wsdl里的targetNamespace里的url", "opName"));
//調用Web Service,傳入參數
String res = ( String ) call.invoke( new Object[]("arg0","arg1"));
System.out.println("===============");
return res;
}
/**
* @param args
*/
public static void main(String[] args) {
try {
System.out.println(getResult());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
} catch (ServiceException e) {
e.printStackTrace();
} catch (SOAPException e) {
e.printStackTrace();
}
}
}
2、利用Eclipse自動生成WebService client代碼就容易多了:(由於還不會發圖片,就用語言描述了,大家酬和看吧。。。)
首先,new project,選擇other,在輸入框中輸入Web Service Client,選中搜索後的結果,點擊Next,在Service definition中輸入 WebService的發布地址,點擊Finish
這樣,WebService Client代碼已經生成好了。
接下來寫一個Test類,在main函數中輸入如下代碼:
String endpoint = "伺服器的WebService的地址";
YourWebServiceNameProxy umsp = new YourWebServiceNameProxy (endpoint);
try {
String resultStr = umsp.opMethod("arg0","arg1");
System.out.println(resultStr);
} catch (Exception e) {
System.out.println("異常");
e.printStackTrace();
} catch (RemoteException e) {
System.out.println("RemoteException異常");
e.printStackTrace();
}
❾ 後台webservice,前台是iOS,在調用借口時出錯,500,有沒有工具可以直接測試介面,就像是postman
soapUI,介面自動化測試工具
❿ ios請求webservice介面的時候,soap請求體怎麼弄的
好了,假設現在你已經有關於soap的基礎知識(沒有也沒關系,看了例子,再理解就更好理解了),下面我們開始做soap的例子.
第一步,建一個Hello_SOAP項目.
然後在Hello_SOAPViewController.h中添加如下代碼
@interface Hello_SOAPViewController : UIViewController
{
IBOutlet UITextField *nameInput;
IBOutlet UILabel *greeting;
NSMutableData *webData;
NSMutableString *soapResults;
NSXMLParser *xmlParser;
BOOL recordResults;
}
@property(nonatomic, retain) IBOutlet UITextField *nameInput;
@property(nonatomic, retain) IBOutlet UILabel *greeting;
@property(nonatomic, retain) NSMutableData *webData;
@property(nonatomic, retain) NSMutableString *soapResults;
@property(nonatomic, retain) NSXMLParser *xmlParser;
-(IBAction)buttonClick: (id) sender;
- (void)getOffesetUTCTimeSOAP;
然後在Hello_SOAPViewController.xib中將兩個輸出口和一個動作連接好,這個不用手把手吧?
在Hello_SOAPViewController.m文件中加入以下方法 :
- (void)getOffesetUTCTimeSOAP
{
recordResults = NO;
//封裝soap請求消息
NSString *soapMessage = [NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<soap:Envelope
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
"<soap:Body>\n"
"<getOffesetUTCTime
xmlns=\"http://www.Nanonull.com/TimeService/\">\n"
"<hoursOffset>%@</hoursOffset>\n"
"</getOffesetUTCTime>\n"
"</soap:Body>\n"
"</soap:Envelope>\n",nameInput.text
];
NSLog(soapMessage);
//請求發送到的路徑
NSURL *url = [NSURL URLWithString:@"http://www.nanonull.com/TimeService/TimeService.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
//以下對請求信息添加屬性前四句是必有的,第五句是soap信息。
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://www.Nanonull.com/TimeService/getOffesetUTCTime" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
//請求
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
//如果連接已經建好,則初始化data
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(@"theConnection is NULL");
}
}
這個方法作用就是封裝soap請求,並向伺服器發送請求.
代
碼有注釋.不重復講解.soap並不難,難的是沒有案例告訴我們怎麼把其它平台的soap移植過來,這里我給出了代碼,我相信對iphone開發人員的話
應該能看懂了.我在下面會把此案例的源代碼附上.如果自己做不出來再看我的代碼.如果我這樣講您覺得不夠細,那說明您的iphone開發還不是太深入,那
么您應該用不到soap技術.可以飄過了.
下面的代碼是接收信息並解析,顯示到用戶界面
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength: 0];
NSLog(@"connection: didReceiveResponse:1");
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
NSLog(@"connection: didReceiveData:2");
}
//如果電腦沒有連接網路,則出現此信息(不是網路伺服器不通)
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"ERROR with theConenction");
[connection release];
[webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"3 DONE. Received Bytes: %d", [webData length]);
NSString
*theXML = [[NSString alloc] initWithBytes: [webData mutableBytes]
length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(theXML);
[theXML release];
//重新載入xmlParser
if( xmlParser )
{
[xmlParser release];
}
xmlParser = [[NSXMLParser alloc] initWithData: webData];
[xmlParser setDelegate: self];
[xmlParser : YES];
[xmlParser parse];
[connection release];
//[webData release];
}
-(void)parser:(NSXMLParser
*)parser didStartElement:(NSString *)elementName namespaceURI:(NSString
*) namespaceURI qualifiedName:(NSString *)qName
attributes: (NSDictionary *)attributeDict
{
NSLog(@"4 parser didStarElemen: namespaceURI: attributes:");
if( [elementName isEqualToString:@"getOffesetUTCTimeResult"])
{
if(!soapResults)
{
soapResults = [[NSMutableString alloc] init];
}
recordResults = YES;
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
NSLog(@"5 parser: foundCharacters:");
if( recordResults )
{
[soapResults appendString: string];
}
}
-(void)parser:(NSXMLParser
*)parser didEndElement:(NSString *)elementName namespaceURI:(NSString
*)namespaceURI qualifiedName:(NSString *)qName
{
NSLog(@"6 parser: didEndElement:");
if( [elementName isEqualToString:@"getOffesetUTCTimeResult"])
{
recordResults = FALSE;
greeting.text
= [[[NSString init]stringWithFormat:@"第%@時區的時間是: ",nameInput.text]
stringByAppendingString:soapResults];
[soapResults release];
soapResults = nil;
NSLog(@"hoursOffset result");
}
}
- (void)parserDidStartDocument:(NSXMLParser *)parser{
NSLog(@"-------------------start--------------");
}
- (void)parserDidEndDocument:(NSXMLParser *)parser{
NSLog(@"-------------------end--------------");
}
說明下:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
這個方法是存儲接收到的信息
-(void)parser:(NSXMLParser
*)parser didEndElement:(NSString *)elementName namespaceURI:(NSString
*)namespaceURI qualifiedName:(NSString *)qName