I'm sending base64 string to php server and its working well. Now I want to send another parameter as a string. Can anyone tell me what code need to add in below code.
Below code is working good for single parameter. How can we modify it for multiple parameters?
NSData *data = [UIImageJPEGRepresentation(imgeview.image,90) base64Encoding];
// Create your request string with parameter name as defined in PHP file
NSString *myRequestString = [NSString stringWithFormat:@"question_image=%@",data];
myRequestString = [myRequestString stringByReplacingOccurrencesOfString:
@"+" withString:@"%2B"];
// Create Data from request
NSData *myRequestData = [NSData dataWithBytes:[myRequestString UTF8String]
length:[myRequestString length]];
request = [[NSMutableURLRequest alloc] initWithURL:
[NSURL URLWithString:@"http://192.168.0.101/Mobile_tutor/webservice/question_details.php"]];
// set Request Type
[request setHTTPMethod:@"POST"];
// Set content-type
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
// Set Request Body
[request setHTTPBody:myRequestData];
// Now send a request and get Response
NSData *returnData = [NSURLConnection sendSynchronousRequest:request
returningResponse:nil
error: nil];
// Log Response
NSString *response = [[NSString alloc] initWithBytes:[returnData bytes]
length:[returnData length]
encoding:NSUTF8StringEncoding];
NSLog(@"-------------%@",response); // here you get reasponse string
Given a
NSDictionary
"params" whose keys and values are strings and where every entry represents one parameter (name/value) you can define a helper category:dataFormURLEncoded
returns a properly encoded character sequence from the given parameters in the dictionary.The encoding algorithm is specified by w3c: URL-encoded form data / The application/x-www-form-urlencoded encoding algorithm
It can be implemented as follows:
First, a helper function which encodes a parameter name, respectively a parameter value:
Finally,
dataFormURLEncoded
composes the character sequence of the encoded parameters. A "parameter" will be composed by concatenating the encodedname
,=
and encodedvalue
:Then, the parameter list will be composed by concatenating the parameters by separating them by a "&":
It can be implemented as below:
Note: The character sequence encodes the strings using Unicode UTF-8.
Example:
Given your parameters:
Now,
encodedParamData
will be added to your body whose content type isapplication/x-www-form-urlencoded
.The encoded parameter string becomes:
a=a+a&b=b%2Bb&c=%C3%BC+%C3%B6
For the network operation these is better supporting API like AFNetworking available witch work async and way better to handle
Tutorials for AFNetworking
Get from here