怎样获取ASIFormDataRequest的postvalue

Jobsz 发布于 2013/07/24 20:26
阅读 1K+
收藏 1
在ios客户端使用ASIFormDataRequest的setPostValue函数将数据发到服务器,服务器用Java(servlet或者struts),怎么才能在服务器端收到来自ios客户端的数据呢?
加载中
0
Jobsz
Jobsz

iOS客户端


- (IBAction)sendPostValue:(id)sender {
    NSLog(@"send test");
    NSURL *url = [NSURLURLWithString:@"http://localhost:8080/server/register"];
    ASIHTTPRequest *request = [ASIHTTPRequestrequestWithURL:url];
    NSString *string = @"{\"name1\":\"zcq喘着粗气\"}";
    NSMutableData *data = [NSMutableDatadata];
    [data appendData:[string dataUsingEncoding:NSUTF8StringEncoding]];
    [request setRequestMethod:@"POST"];
    [request appendPostData:data];
    [request setDelegate:self];
    [request setDidFinishSelector:@selector(finish:)];
    [request setDidFailSelector:@selector(failFinish:)];
    [request startAsynchronous];

}



Java服务器端


publicvoid doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
BufferedReader br = new BufferedReader(new InputStreamReader((ServletInputStream)request.getInputStream(),"utf-8"));
         String line = null;
         StringBuilder sb = new StringBuilder();
         while((line = br.readLine())!=null){
         System.out.println(line);
             sb.append(line);
         }
         System.out.println(sb.toString());
         out.print("sb = "+sb.toString());
         ZCQ o = JSON.parseObject(sb.toString(),ZCQ.class);
         System.out.println(o.getName1());
         out.print(o.getName1());
out.flush();
out.close();
}
Railgun
Railgun
这不是FormRequest吧……
0
红薯
红薯
request 有读取body的方法,去api文档找
Jobsz
Jobsz
红薯哥,已经完成了,实现了可以取出来了
Jobsz
Jobsz
搞定了。取出来了。不过客户端到服务器的数据有编码问题,好像都被URLencode了。不知道咋个处理,我试了几个方法都不行
Jobsz
Jobsz
谢谢红薯哥哈
Jobsz
Jobsz
哦哦,好我在查一下
红薯
红薯
@brucezcq 当然不是,你找一下不要偷懒,我在外面没法看,似乎是 getInputStream
下一页
0
wwwang89
wwwang89
好文章,要ddddd
返回顶部
顶部