-1

POST メソッドを使用して JSON 文字列をサーバーに送信しています。「Array(JSON String) Array(JSON String)」を示す応答を返す必要があります。応答には 2 つの配列が含まれています。最初の配列は POST メソッドを使用した場合に入力され、2 番目の配列は GET メソッドを使用した場合に入力されます。GET メソッドを使用して JSON を送信しようとしましたが、実際には 2 番目の配列に値が入力されました。しかし、POST メソッドで JSON を送信しようとすると、両方の配列が空で返されます。

使用したコードは次のとおりです。

NSData *requestData = [NSJSONSerialization dataWithJSONObject:sqlArray オプション:NSJSONWritingPrettyPrinted エラー:&error];

NSURL *url = [NSURL URLWithString:@"http://myurl/xmlrpc/imwebsrvcjson.php"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];

[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:requestData];

NSData *result =[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSString *returnString = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];

if (error == nil){
    NSLog(@"The Result string: %@", returnString);   
}

私のコードの何が問題なのか教えていただけますか?

4

1 に答える 1

-2

これが私がすることです(私のサーバーに送られるJSONは、 key = question..ie {:question => { dictionary } } に対して1つの値を持つ辞書(別の辞書)である必要があることに注意してください):

NSArray *objects = [NSArray arrayWithObjects:[[NSUserDefaults standardUserDefaults]valueForKey:@"StoreNickName"],
  [[UIDevice currentDevice] uniqueIdentifier], [dict objectForKey:@"user_question"],     nil];
NSArray *keys = [NSArray arrayWithObjects:@"nick_name", @"UDID", @"user_question", nil];
NSDictionary *questionDict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

NSDictionary *jsonDict = [NSDictionary dictionaryWithObject:questionDict forKey:@"question"];

NSString *jsonRequest = [jsonDict JSONRepresentation];

NSLog(@"jsonRequest is %@", jsonRequest);

NSURL *url = [NSURL URLWithString:@"https://xxxxxxx.com/questions"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
             cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];


NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];

[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];

NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if (connection) {
 receivedData = [[NSMutableData data] retain];
}
The receivedData is then handled by:

NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [jsonString JSONValue];
NSDictionary *question = [jsonDict objectForKey:@"question"];

これは 100% 明確ではなく、再読する必要がありますが、開始するためにすべてがここにあるはずです。そして、私が知る限り、これは非同期です。これらの呼び出しが行われている間、UI はロックされません。それが役立つことを願っています。

于 2012-12-01T10:43:54.897 に答える