我的客户为我的注册提供了一个Web服务。我需要发布值。 我使用以下代码发布:
-(IBAction)testingPurpose:(id)sender{
NSMutableDictionary *finalQuoteDict = [[NSMutableDictionary alloc] init];
[finalQuoteDict setValue:@"It is an Error Message" forKey:@"ErrorMsg"];
[finalQuoteDict setValue:@"json" forKey:@"ReturnVal"];
[finalQuoteDict setValue:@"john@live.com" forKey:@"Email"];
[finalQuoteDict setValue:@"David John" forKey:@"FullName"];
[finalQuoteDict setValue:@"2147483647" forKey:@"UserID"];
[finalQuoteDict setValue:@"john" forKey:@"UserName"];
[finalQuoteDict setValue:@"qqqqqq" forKey:@"UserPassword"];
SBJsonWriter *jsonWriter = [[SBJsonWriter alloc] init];
NSString *jsonRequest = [jsonWriter stringWithObject:finalQuoteDict];
jsonRequest = [jsonRequest stringByReplacingOccurrencesOfString:@"<" withString:@""];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@Register",MainUrl1,jsonRequest]];
NSLog(@"url is---%@",url);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];
NSError* error = nil;
NSURLResponse* response;
NSData* result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *dataString=[[NSString alloc]initWithData:result encoding:NSUTF8StringEncoding];
NSMutableDictionary *getResponseDict = [[NSMutableDictionary alloc] init];
[getResponseDict addEntriesFromDictionary:[dataString JSONValue]];
}
但它抛出一个错误说 &#34;错误跟踪是:( &#34;错误域= org.brautaset.JSON.ErrorDomain Code = 3 \&#34;无法识别的前导字符\&#34; UserInfo = 0x856ac50 {NSLocalizedDescription =无法识别的主角}&#34;
请检查图像,即发布值..
我们是否需要提供请求类型&#34; json / xml&#34;
提前多多感谢
答案 0 :(得分:0)
除非或直到webservice明确要求,否则您不需要在请求类型中提供json或xml。如果webservice处理它会更好。
但我认为问题在于解析响应。
首先确保dataString
不为空或空,或者您从服务器获得一些响应。
如果它有一些值,那么返回值或服务器响应可能不是有效的JSON格式。
您可以通过粘贴json来验证json响应 http://jsonlint.com/。
答案 1 :(得分:0)
尝试使用此功能 这里httpMethod是“POST”。 postData是所有ur发布数据,其Key值与Web服务请求相同。
aUrl是你的服务网址
-(void)WebService:(NSString *)httpMethod DataDictionary:(id)postData RequestAction:(NSString *)aUrl
{
// Check internet Connection
//Use Reachability class for this==============
Reachability *r = [Reachability reachabilityWithHostName:@"www.google.com"];
NetworkStatus internetStatus = [r currentReachabilityStatus];
if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN))
{
NSLog("No Internet Connection Available");
return;
}
self.identifier = serviceIdentifier;
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:aUrl] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0 ];
NSLog(@"final request is %@",request);
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
//set body in JSON formate
[request setHTTPBody:[[self convertToJSON:postData] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *contentLength = [NSString stringWithFormat:@"%d",[[request HTTPBody] length]];
[request setValue:contentLength forHTTPHeaderField:@"Content-Length"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection)
{
self.responseData = [NSMutableData data]; //Its a mutable Data object
}
}
//Convert data into JSOn format
//use JSON classes for this
-(NSString *)convertToJSON:(id)requestParameters
{
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:requestParameters options:NSJSONWritingPrettyPrinted error:nil];
NSLog(@"JSON DATA LENGTH = %d", [jsonData length]);
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"JSON STR LENGTH = %d", [jsonString length]);
return jsonString;
}
//您将在NSURLConnection中获得回复
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[self.responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog("Failed to get Result......");
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *jsonString = [[[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding] autorelease];
NSDictionary *aDic = [jsonString JSONValue];
NSLog("Your Response Data is ==>> %d",aDic);
}
希望这会对你有所帮助