我只是想解析URL中的JSON, 下面的代碼寫在ObjC中運行良好,給我正確的響應, ,但代碼寫在swift 3.1中,沒有給出正確的響應。 Giving me 500 Internal Error
在ObjC中工作,而不是迅速。什麼是錯誤的,我正在用快速解析JSON解析
我在做什麼錯。 任何人都可以指導。 在此先感謝。
我也嘗試使用轉換器從ObjC到Swift。 但它失敗了一些錯誤,雖然我解決了這些錯誤,但仍然沒有給出正確的輸出。
下面是ObjC代碼。
-(void)comparedatainserturl
{
NSError *error;
NSMutableArray *arr=[[NSMutableArray alloc]init];
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
[dict setObject:@"e639e129d0f3ab7d" forKey:@"imei_no"];
[dict setObject:@"2017-08-29 12:18:44" forKey:@"current_time"];
[dict setObject:@"0" forKey:@"tbl_offer_details"];
[dict setObject:@"0" forKey:@"tbl_card_type"];
[dict setObject:@"0" forKey:@"tbl_dashboard"];
[dict setObject:@"0" forKey:@"tbl_category_details"];
[dict setObject:@"0" forKey:@"tbl_sub_category_details"];
[dict setObject:@"0" forKey:@"tbl_payment_option"];
[dict setObject:@"0" forKey:@"tbl_payment_option_provider"];
[dict setObject:@"0" forKey:@"tbl_like_favorite_share"];
[dict setObject:@"0" forKey:@"tbl_user_details"];
[arr addObject:dict];
NSLog(@"arra :- %@",[arr description]);
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSString *string = [NSString stringWithFormat:@"data=%@",jsonString];
NSLog(@"jsonData as string:\n%@", string);
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
NSString *urlString=[NSString stringWithFormat:@"%@",@"http://admin.scontos.com/index.php/Android_api/Offers_download"];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: data];
NSError *err;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];
NSLog(@"gotresponsestring=%@", resSrt);
}
低於銀行代碼,
func comparedatainserturl() {
var arrayParams : NSDictionary = [:]
arrayParams = ["imei_no":"e639e129d0f3ab7d","current_time":"2017-08-2911:08:26","tbl_offer_details":"0","tbl_card_type":"0","tbl_dashboard":"0","tbl_category_details":"0","tbl_sub_category_details":"0","tbl_payment_option":"0","tbl_payment_option_provider":"0","tbl_like_favorite_share":"0","tbl_user_details":"0"]
print(arrayParams)
let valueArr : Array = [arrayParams]
print(valueArr)
let jsonData: Data? = try! JSONSerialization.data(withJSONObject: valueArr, options: .prettyPrinted)
let jsonString = String(data: jsonData!, encoding: String.Encoding.utf8)
let string: String = "data=\(jsonString)"
print("jsonData as string:\n\(string)")
let dataa: Data? = string.data(using: String.Encoding.utf8)
var _: NSError?
let url1:URL = URL(string:"http://admin.scontos.com/index.php/Android_api/Offers_download")!
let request:NSMutableURLRequest = NSMutableURLRequest(url: url1)
request.httpMethod = "POST"
request.httpBody = dataa
var response: URLResponse?
do{
let urlData: Data? = try NSURLConnection.sendSynchronousRequest(request as URLRequest, returning:&response)
let resstr = NSString(data: urlData!, encoding: String.Encoding.utf8.rawValue)
print("Response is \(resstr! as String)")
}catch{
print(error)
}
}
你嘗試過什麼基本的調試策略? –
順便說一句,我建議不要使用ObjC - > Swift轉換器,如果可能的話。它們會生成直接翻譯,這會導致代碼非常糟糕。就像期待Google Translate將一首法語詩一樣轉換成英文。當然,所有的單詞都會在那裏,但它會是一首可怕的詩。 – Rob
你的網址是否有效?因爲我收到了一些數據庫錯誤。 –