1)On Iphone,
- (void) postData:(NSMutableData *)_body withAction:(NSString *)_action binary:(BOOL)_binary
{
[self stopProcess];
binary = _binary;
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
NSString *url = [NSString stringWithFormat:API_FORMAT, APP_SERVER, _action, [self getSession]];
if (debug_switch) {
NSLog(@"The action is %@", _action);
NSLog(@"The accessing server API call Datafeed is %@", url);}
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
//TRACE(@"url: %@", url);
if(_body != nil)
{
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
[request setHTTPBody:_body];
}
//con = [NSURLConnection connectionWithRequest:request delegate:self];
if(con != nil) {
[con release];
}
con = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (con)
{
dataDict = nil;
loading = YES;
if(receivedData == nil) {
receivedData = [[NSMutableData data] retain];
}
[receivedData setLength:0];
}
}
static NSString *boundary = @"---------------------------147378274664144922";
@implementation DataFeed
BOOL connectable = NO;
///////////////////////////////
- (NSMutableData *) initContentBody
{
NSMutableData *body = [NSMutableData data];
[body appendData:[self addFormData:@"uid" withString:[[UIDevice currentDevice] uniqueIdentifier]]];
return body;
}
//////////////////////////
- (NSData *) addFormBoundary
{
return [[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding];
}
//////////////////////////////////////////
- (NSData *) addFormData:(NSString *)_name withInt:(int)_value
{
return [self addFormData:_name withString:[[NSNumber numberWithInt:_value] stringValue]];
}
- (NSData *) addFormData:(NSString *)_name withFloat:(float)_value
{
return [self addFormData:_name withString:[[NSNumber numberWithFloat:_value] stringValue]];
}
- (NSData *) addFormData:(NSString *)_name withString:(NSString *)_value
{
NSMutableData *body = [NSMutableData data];
[body appendData:[self addFormBoundary]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\";\r\n\r\n%@", _name, _value] dataUsingEncoding:NSUTF8StringEncoding]];
return body;
}
- (NSData *) addFormData:(NSString *)_name filename:(NSString *)_filename withData:(NSData *)_data
{
NSMutableData *body = [NSMutableData data];
[body appendData:[self addFormBoundary]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", _name, _filename] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/zip\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Transfer-Encoding: binary\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:_data];
return body;
}
2)On Android,
public void executeHttpPost() throws Exception {
String address = "";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(address);
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("key1", "value1"));
pairs.add(new BasicNameValuePair("key2", "value2"));
post.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse response = client.execute(post);
}
The question, on body data of the android can it set to non json format, key-value pair? as you can see on iphone, body can be any parameters, doesn’t have to be key value pair.can you give an example on android where the post data can be non-json format?
You’re not constrained to
UrlEncodedFormEntity, check out the “Known Indirect Subclasses” of theorg.apache.http.HttpEntityinterface (at the top of the page).The most commonly used ones are probably:
ByteArrayEntity: An entity whose content is retrieved from a byte array.FileEntity: An entity whose content is retrieved from a file.InputStreamEntity: A streamed entity obtaining content from anInputStream.SerializableEntity: Takes anSerializableobject and outputs its serialized formStringEntity: An entity whose content is retrieved from a string.UrlEncodedFormEntity: An entity composed of a list of url-encoded pairs.(this list is not complete, check out the above link)
Here’s some examples of how to use different types of entities:
Of course, don’t do this all at once, one call to
setEntity()only! If you need MIME multipart requests, check out this tutorial by Vikas Patel (you’ll need an updated Apache HTTP Client JAR).