gpt4 book ai didi

android - HttpPost Iphone 与 Android

转载 作者:行者123 更新时间:2023-11-29 22:19:32 25 4
gpt4 key购买 nike

1)在苹果手机上,

- (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)在安卓上,

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);

}

问题,android的body data可以设置成非json格式,键值对吗?正如您在 iphone 上看到的那样,body 可以是任何参数,不一定是键值对。您能举一个在 android 上发布数据可以是非 json 格式的例子吗?

最佳答案

您不限于 UrlEncodedFormEntity,查看 org.apache.http.HttpEntity 的“已知间接子类”界面(在页面顶部)。

最常用的大概是:

  • ByteArrayEntity:一个实体,其内容是从字节数组中检索的。
  • FileEntity:从文件中检索内容的实体。
  • InputStreamEntity:从 InputStream 获取内容的流实体。
  • SerializableEntity:获取一个Serializable对象并输出其序列化形式
  • StringEntity:从字符串中检索内容的实体。
  • 最后是 UrlEncodedFormEntity:一个由 url 编码对列表组成的实体。

(此列表不完整,请查看上面的链接)

下面是一些如何使用不同类型实体的示例:

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(address);

// a string entity containing JSON:
post.setEntity(new StringEntity("{ \"actually\" : [\"json\", \"this time\"]}");

// or uploading an image file:
post.setEntity(new FileEntity(new File("some/local/image.png"), "image/png");

// or some random bytes:
byte[] randomBytes = new byte[128];
new Random().nextBytes(randomBytes);
post.setEntity(new ByteArrayEntity(randomBytes);

HttpResponse response = client.execute(post);
...

当然,不要一次完成所有这些,一次调用 setEntity() 即可!如果您需要 MIME 多部分请求,请查看 this tutorial by Vikas Patel (您需要更新的 Apache HTTP 客户端 JAR)。

关于android - HttpPost Iphone 与 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7613713/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com