我有以下字典:
NSDictionary *param = @{@"schoolid":@"schooldb1",
@"token":del.tokenString,
@"mobile":del.phoneString
};
NSLog(@"param:%@",param);
我想将此参数(schoolid,token,mobile)发送到Web视图。但是我不知道该怎么发送。我试图在互联网上搜索,但没有得到解决我问题的合适方法。
我的主要网址是:
NSString *url=@"https://MyURL.com/School/AppSingleTrack";
我将像下面这样调用UIWebview:
NSString *finalurl=[NSString stringWithFormat:@"https://MyURL.com/School/AppSingleTrack/?%@",param];
NSURL *nsurl=[NSURL URLWithString:finalurl];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[_webview loadRequest:nsrequest];
[self.view addSubview:_webview];
使用此代码,
NSString *sUrl = @"https://24x7tracker.com/School/AppSingleTrack";
NSMutableURLRequest *res = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:sUrl]];
[res setHTTPMethod:@"POST"];
NSDictionary *params; = [NSDictionary dictionaryWithObjectsAndKeys:
@"schooldb1",@"schoolid",
del.tokenString,@"token",
del.phoneString,@"mobile",
nil];
NSMutableArray *pairArray = [[NSMutableArray alloc] initWithCapacity:0];
for (NSString *key in params)
[pairArray addObject:[NSString stringWithFormat:@"%@=%@", key, params[key]]];
[res setHTTPBody:[[pairArray componentsJoinedByString:@"&"] dataUsingEncoding:NSUTF8StringEncoding]];
[NSURLConnection sendAsynchronousRequest:res
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSLog(@"request URL : %@",res.URL);
NSLog(@"request Method : %@",res.HTTPMethod);
NSLog(@"parameters : %@",params);
NSLog(@"response : %@",response);
Resp *r = [ [Resp alloc] initWithDictionary:[NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error:nil]];
// Success - Show Sucess message
if ([r.sCode isEqualToString:@"success"]) {
NSLog(@"response message : %@",r.sData);
}
}];
使用班级代表:
响应时间
#import <Foundation/Foundation.h>
@interface Resp : NSObject
@property (nonatomic, copy) NSString *sCode;
@property (nonatomic, copy) NSString *sMessage;
@property (nonatomic, copy) NSString *sData;
- (id)initWithDictionary:(NSDictionary *)dictionary;
@end
响应
#import "Resp.h"
@implementation Resp
@synthesize sCode = _id;
@synthesize sMessage = _title;
@synthesize sData = _data;
- (id)initWithDictionary:(NSDictionary *)dictionary {
self = [super init];
if (self) {
self.sCode = [dictionary objectForKey:@"code"];
self.sMessage = [dictionary objectForKey:@"message"];
self.sData = [dictionary objectForKey:@"data"];
}
return self;
}
@end
然后终于得到回应,希望对您有所帮助
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句