だから私たちは同じページにいます:私はNSData+GZIP、 JSONKit 、およびNSData+Base64を使用しています
タイトルが示すように、ここでの私の目標は、json オブジェクトを取得し、圧縮し、base64 でエンコードし、フォーム ポストとして .net サーバーに送信することです。フォーム ポストは、このデータを取得し、プロセスを逆にして終了することを期待しています。 json オブジェクトを使用します。
AndroidとiOSの両方でこれを行う必要があります。アンドロイドが完成し、うまく動作します。参考までに、私がそこでやっていることは次のとおりです。
public String compress(String string) throws IOException {
byte[] blockcopy = ByteBuffer.allocate(4).order(java.nio.ByteOrder.LITTLE_ENDIAN).putInt(string.length()).array();
ByteArrayOutputStream os = new ByteArrayOutputStream(string.length());
GZIPOutputStream gos = new GZIPOutputStream(os);
gos.write(string.getBytes());
gos.close();
os.close();
byte[] compressed = new byte[4 + os.toByteArray().length];
System.arraycopy(blockcopy, 0, compressed, 0, 4);
System.arraycopy(os.toByteArray(), 0, compressed, 4, os.toByteArray().length);
StringBuffer hexString = new StringBuffer();
for (int i=0; i<compressed.length; i++)
hexString.append(Integer.toHexString(0xFF & compressed[i]));
return Base64.encodeBytes(compressed);
}
成功するiOSでのテストは次のとおりです。
NSLog(@"test? %@", [[NSString alloc]initWithData:[[NSData dataFromBase64String:[[[@"test" dataUsingEncoding:NSUTF8StringEncoding] gzippedData] base64EncodedString]] gunzippedData] encoding:NSUTF8StringEncoding]);
これは次のように表示されます:
"test? test"
気になる部分:
NSStringEncoding encoding = NSUTF8StringEncoding;
NSString* string = [[NSArray arrayWithArray:mutableData] JSONString];
NSData* data = [string dataUsingEncoding:encoding];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:[NSString stringWithFormat:@"http://www.google.com/myendpoint.aspx?deviceid=%@&accesstoken=%@", [defaults objectForKey:@"deviceId"], [defaults objectForKey:@"ggAccessToken"]]]];
[request setHTTPMethod:@"POST"];
NSMutableData *requestBodyData = nil;
requestBodyData = [NSMutableData data];
// Create boundry
NSString *boundary = [NSString stringWithFormat:@"--%@--", [[NSProcessInfo processInfo] globallyUniqueString]];
[request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"];
NSData *boundaryData = [[[@"--" stringByAppendingString:boundary] stringByAppendingString:@"\r\n"] dataUsingEncoding:encoding];
// Add Data to POST
[requestBodyData appendData:boundaryData];
[requestBodyData appendData:[[NSString stringWithFormat:@"Content-Disposition: multipart/form-data; "] dataUsingEncoding:encoding]];
[requestBodyData appendData:[[NSString stringWithFormat:@"name=\"%@\"; ", @"contacts"] dataUsingEncoding:encoding]];
[requestBodyData appendData:[[NSString stringWithFormat:@"\r\n\r\n"] dataUsingEncoding:encoding]];
NSString* finalString = [[data gzipDeflate] base64EncodedString];
//an example of finalString at this point: H4sIAAAAAAAAA6WOwQrCMAyGX0VyUqjHOtk7CN5HGekWMdB2knansXc3FcSDKIiHhBC+5P+6BWYJGdrOGbhdp0R1XiCgpwAtnCbPgcBAmqMn0c3WWrvbaNtrWVjNC+ZzffARPsCqIRSRn4EXllz6hJGULcK5YNJzHEehnB8qrgb8o9j8onj8rjigCNObobsD+2WV/kYBAAA=
NSData* finalData = [finalString dataUsingEncoding:NSUTF8StringEncoding];
[requestBodyData appendData:finalData];
[requestBodyData appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:encoding]];
[requestBodyData appendData:boundaryData];
[request setHTTPBody:requestBodyData];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSLog(@"%@", [data objectFromJSONData]);
}];
この時点ではすべて問題ないように見えますが、(.net) サーバーからの結果は次のとおりです。
ERROR TYPE: System.IO.InvalidDataException - ERROR MESSAGE: The magic number in GZip header is not correct. Make sure you are passing in a GZip stream. -
ここにはまだ多くの質問がないことはわかっていますが、1 つだけ質問があります。この時点で、誤って圧縮されたデータを生成するために何ができるでしょうか? (Androidが完全に機能し、その側を構築した人がかなり経験豊富であることを考えると、現時点ではサーバー側が正しいと仮定しましょう)...