0

我正在制作一个通过 API 与数据库通信的 iOS 应用程序。API 将有效的 JSON 发送到应用程序,但应用程序没有给出错误但另一个结果:NULL。

这是我的 iOS 应用程序代码:

 // Start hud
 MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
 hud.labelText = @"Zoeken...";

 return TRUE;
 }

    - (void)requestFinished:(ASIHTTPRequest *)request
   {    
 [MBProgressHUD hideHUDForView:self.view animated:YES];
if (request.responseStatusCode == 400) {
    textView.text = @"Invalid code";        
} else if (request.responseStatusCode == 403) {
    textView.text = @"Code already used";
} else if (request.responseStatusCode == 204) {
    textView.text = @"No content";
} else if (request.responseStatusCode == 412) {
    textView.text = @"Precondition Failed";
} else if (request.responseStatusCode == 200) {
    NSString *responseString = [request responseString];
    NSDictionary *responseDict = [responseString JSONValue];
    // NSString *naam = [responseDict objectForKey:@"DEB_NR"];

    NSString *part0 = [responseDict objectForKey:@"klntnr"];
    NSString *part1 = [responseDict objectForKey:@"klntnm"];
    NSString *part2 = [responseDict objectForKey:@"adrs"];
    NSString *show = [NSString stringWithFormat:@"\r%@\r%@\r%@" , part0 , part1 , part2 ];

   // if ([unlockCode compare:@"com.razeware.test.unlock.cake"] == NSOrderedSame) {
        textView.text = [NSString stringWithFormat:@"Resultaten: %@", show];
  //  } else {
       // textView.text = [NSString stringWithFormat:@"Resultaat: %@", unlockCode];
  //  }

  } else {
      textView.text = @"Unexpected error API ERROR";
  }

}

 - (void)requestFailed:(ASIHTTPRequest *)request
 {    
 [MBProgressHUD hideHUDForView:self.view animated:YES];
 NSError *error = [request error];
  textView.text = error.localizedDescription;
  }

 @end

提前致谢,

莫里斯。

4

1 回答 1

-1

如果您只显示 JSON 字符串,那将非常有帮助;但是试试这个

else if (request.responseStatusCode == 200) {

NSError* error;
NSString *responseString = [request responseString];
NSArray *responseObj = [NSJSONSerialization JSONObjectWithData: [responseString dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableContainers error: &error];
NSString *part0=@"";
NSString *part1=@"";
NSString *part2=@"";

for (int i=0; i<[responseObj count]; i++) {
    NSDictionary *dataDict = [responseObj objectAtIndex:i];
    NSString *part0 = [dataDict objectForKey:@"klntnr"];
    NSString *part1 = [dataDict objectForKey:@"klntnm"];
    NSString *part2 = [dataDict objectForKey:@"adrs"];
}
NSString *show = [NSString stringWithFormat:@"\r%@\r%@\r%@" , part0 , part1 , part2 ];
textView.text = [NSString stringWithFormat:@"Resultaten: %@", show];
}
于 2013-05-21T14:24:31.240 回答