`HttpPost Java (Android) для ObjC (IOS)
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(urls[0]);
JSONObject holder = new JSONObject();
JSONObject userObj = new JSONObject();
String response = null;
JSONObject json = new JSONObject();
try {
try {
// setup the returned values in case
// something goes wrong
json.put("success", false);
json.put("info", "something wrong.");
// add the users's info to the post params
userObj.put("email", mEmail);
userObj.put("password", mPassword);
holder.put("user", userObj);
StringEntity se = new StringEntity(holder.toString(), "utf-8");
post.setEntity(se);
// setup the request headers
post.setHeader("Accept", "application/json");
post.setHeader("Content-Type", "application/json");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
response = client.execute(post, responseHandler);
json = new JSONObject(response);`
.......
==========
Up, исходный код Java. написанный кем-то. Вниз, источник кода Objective-C. (Request является указателем NSMutableURLRequest), я набираю этот код.
===========
NSMutableDictionary *datas = [[NSMutableDictionary alloc]init];
[datas setValue:@"[email protected]" forKey:@"email"];
[datas setValue:@"zxcvbnmm" forKey:@"password"];
NSString *httpbody = [NSString stringWithFormat:@"user=%@",datas];
NSData *httpbodydata = [httpbody dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:httpbodydata];
NSString *length = [NSString stringWithFormat:@"%d",[httpbodydata length]];
[request addValue:length forHTTPHeaderField:@"Content-Length"];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"application/json"] forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"application/json"] forHTTPHeaderField:@"Accept"];
// Perform request and get JSON back as a NSData object
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
===========
Что отличается от того, что два кода?
в результате, Java работает отлично, но Objc can not send json object!
Я просто хочу отправить, как Java-код.
и, пожалуйста, скажите мне, что StringEntity делает в этом java-коде.