iOS json登录服务器
来源:互联网 发布:教育部网络培训中心 编辑:程序博客网 时间:2024/06/06 09:32
1.导入JSONKit.h JSONKit.m
2.ViewController.m中引用头文件
#import "JSONKit.h"
3.
登录按钮中写:
- (IBAction)login:(UIButton *)sender {
NSString *urlString =@"http:……";
NSURL *url = [NSURLURLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];
[requestsetTimeoutInterval:5.0];
[requestsetHTTPMethod:@"post"];
NSString *bodyString =@"发送内容一般是有格式的用户名和密码";
NSData *body = [bodyStringdataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"bodystring %@", bodyString);
[requestsetHTTPBody:body];
NSURLConnection *conn = [[NSURLConnectionalloc]initWithRequest:requestdelegate:self];
[connstart];
}
}
4.下列函数写入ViewController.m,与- (void)viewDidLoad 并列
#pragma mark - 连接代理方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"开始接受服务器的返回请求");
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"1111111:");
_serverData = [NSMutableDatadata];
[_serverDataappendData:data];
NSLog(@"中间接收的数据:%@", data);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *string = [[NSStringalloc]initWithData:_serverDataencoding:NSUTF8StringEncoding];
NSError *error =nil;
id jsonObject = [NSJSONSerializationJSONObjectWithData:_serverDataoptions:NSJSONReadingAllowFragmentserror:&error];
if (jsonObject != nil && error == nil)
{
NSLog(@"Successfully deserialized...");
if ([jsonObject isKindOfClass:[NSDictionaryclass]])
{
NSDictionary *deserializedDictionary = (NSDictionary *)jsonObject;
NSLog(@"Dersialized JSON Dictionary = %@", deserializedDictionary);
}
}
}
// 网络请求失败
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"失败 %@", error.localizedDescription);
_serverData = nil;
}
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
{
NSLog(@"发送数据 %ld %ld %ld", (long)bytesWritten, (long)totalBytesWritten, (long)totalBytesExpectedToWrite);
}
5.结束 测试
- iOS json登录服务器
- iOS XMPP Framework 05 - 登录服务器
- iOS 获取服务器数据(json)
- iOS开发-给服务器发送JSON数据
- iOS 构造json并提交到服务器
- Unity登录服务器及处理返回的json数据
- iOS传输Json数据(纯Json)到服务器
- iOS开发之注册登录 (需要服务器支持)
- ios-day23-02(ios开发之json的反序列化、提交json数据到服务器)
- IOS笔记之,post一个Json到服务器
- iOS 学习日志 : 向服务器发送Json数据
- iOS post一个json字符串到服务器 评论
- iOS开发往服务器Post一个json数据
- iOS开发中上传JSON字符串到后台服务器
- ios异步登录,登录的是php写服务器,同理可以实现文件图片下载
- iOS客户端发送json数据,java(servlet)服务器接受json数据
- iOS 登录
- json web token登录
- 优化命题的对偶性(Duality)
- oracle聚簇
- cc2430的中文资料
- CGContextAddArc与CGContextAddArcToPoint参数解析
- Animation介绍加动画属性的详解
- iOS json登录服务器
- 通信中英对照表
- leetcode--Simplify Path
- 简单的JavaScript组件化实现
- SQL Server date、datetime、smalldate区别
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault)
- getting multivariate gaussian distribution from independent ones
- POJ3243 Clever Y 解 高次同余方程
- 矩形面积并 hdu 1542 Atlantis