ios客户端与java服务器进行通信

客户端代码是这样的。。。

#import "ViewController.h"

@implementation ViewController

-(void)aaa:(UIButton *)btn
{
    NSString *method=[NSString stringWithFormat:@"login"];
    NSString *username=[NSString stringWithFormat:@"123"];
    NSString *password=[NSString stringWithFormat:@"123"];
    NSString *urlString= [NSString stringWithFormat:@"http://192.168.1.107:8080/ipTest/test.do?%@%@&%@%@&%@%@",@"method=",method,@"username=",username,@"password=",password];
  
    ASIFormDataRequest *requestForm = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
    //设置需要POST的数据,这里提交两个数据,A=a&B=b
    //[requestForm setPostValue:@"a" forKey:@"A"];
    //[requestForm setPostValue:@"b" forKey:@"B"];
    [requestForm startSynchronous];
    
    //输入返回的信息
    NSLog(@"response\n%@",[requestForm responseString]);
    [requestForm release];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    button1=[[UIButton alloc]initWithFrame:CGRectMake(200, 200, 50, 50)];
    button1.backgroundColor=[UIColor redColor];
    [self.view addSubview:button1];
    [button1 addTarget:self action:@selector(aaa:) forControlEvents:UIControlEventTouchUpInside];
// Do any additional setup after loading the view, typically from a nib.

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end
客户端想要访问服务器 必须加上服务器的ip 服务端我是这样实现的。。。
服务器是用java开发的
public void doLogin(HttpServletRequest request,HttpServletResponse response) throws IOException{
  String username=request.getParameter("username");
  String password=request.getParameter("password");
  String getStr=request.getParameter("A");
  System.out.println("用户名:"+username+ "密码:"+password);
  getAddr(request);
  PrintWriter out=response.getWriter();
  String msg=null;
  if(username!=null&&username.equals("123")&&password!=null &&password.equals("123")&& getStr!=null&&getStr.equals("a")){
   msg="登陆成功";
  }
  else
  {
   msg="登陆失败";
  }
 
  out.print(msg);
  out.flush();
  out.close();
 }
 
 

转载于:https://my.oschina.net/xzs1913/blog/59512

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值