简体   繁体   English

iPhone-在POST中将JSON对象发送到PHP服务器并从服务器获取响应

[英]iPhone- Send JSON Object to PHP server in POST and get response from server

Im trying to send JSON object to php server from my iphone 我试图从我的iPhone发送JSON对象到PHP服务器

The server will decode it do some functions and then replay with JSON back to iphone. 服务器会解码它做一些功能,然后用JSON重播回iphone。

I have the the server set up but im not sure what methods I need to use to connect to the php server (using POST method) and also how the iphone will get the response from the server. 我有服务器设置,但我不知道我需要使用什么方法连接到php服务器(使用POST方法)以及iphone将如何从服务器获得响应。

here is your code enjoy 这是你的代码享受

NSString *urlString = @"Your url"; NSString * urlString = @“你的网址”;

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSMutableData *body = [NSMutableData data];


NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];




[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];



// Text parameter1
// NSString *param1 = @"parameter text";
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"name\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:param1] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];



// Another text parameter
// NSString *param2 = @"Parameter 2 text";
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"desc\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:param2] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];


// close form
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// set request body
[request setHTTPBody:body];

like above code u can send the multiple value using HTTP post method. 像上面的代码你可以使用HTTP post方法发送多个值。

Check this out for POSTing data: iPhone sending POST with NSURLConnection 检查一下POST数据: iPhone用NSURLConnection发送POST

How can you have a JSON object in Cocoa? 你如何在Cocoa中拥有一个JSON对象? You somehow have to convert whatever data you have to JSON, send it to the server and then parse the answer. 你不得不将你拥有的任何数据转换为JSON,将其发送到服务器,然后解析答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM