我需要在從web獲得響應後,使用post方法顯示鍵(貨幣)的特定對象。客戶端服務器json響應
#import "ViewController.h"
@interface ViewController()
@end
@implementation ViewController{
NSMutableData *mutableData;
NSMutableString *arr;
#define URL @"website"
// change this URL
#define NO_CONNECTION @"No Connection"
#define NO_VALUES @"Please enter parameter values"
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(IBAction)sendDataUsingPost:(id)sender{
[self sendDataToServer :@"POST"];
}
-(IBAction)sendDataUsingGet:(id)sender{
[self sendDataToServer : @"GET"];
}
-(void) sendDataToServer : (NSString *) method{
NSString *[email protected]"3";
serverResponse.text = @"Getting response from server...";
NSURL *url = nil;
NSMutableURLRequest *request = nil;
if([method isEqualToString:@"GET"]){
NSString *getURL = [NSString stringWithFormat:@"%@?branch_id=%@", URL, Branchid];
url = [NSURL URLWithString: getURL];
request = [NSMutableURLRequest requestWithURL:url];
NSLog(@"%@",getURL);
}else{ // POST
NSString *parameter = [NSString stringWithFormat:@"branch_id=%@",Branchid];
NSData *parameterData = [parameter dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
url = [NSURL URLWithString: URL];
NSLog(@"%@", parameterData);
request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPBody:parameterData];
arr= [NSMutableString stringWithUTF8String:[parameterData bytes]];
NSLog(@"responseData: %@", arr);
//NSLog(@"%@",[[arr valueForKey:@"BranchByList"]objectForKey:@"currency"]);
}
[request setHTTPMethod:method];
[request addValue: @"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
//NSLog(@"%@",[connection valueForKeyPath:@"BranchByList.currency"]);
if(connection)
{
mutableData = [NSMutableData new];
//NSLog(@"%@",[connection valueForKeyPath:@"BranchByList.currency"]);
}
}
-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)response
{
[mutableData setLength:0];
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[mutableData appendData:data];
}
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
serverResponse.text = NO_CONNECTION;
return;
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSMutableString *responseStringWithEncoded = [[NSMutableString alloc] initWithData: mutableData encoding:NSUTF8StringEncoding];
//NSLog(@"Response from Server : %@", responseStringWithEncoded);
NSLog(@"%@",responseStringWithEncoded );
NSLog(@"%@",[responseStringWithEncoded valueForKeyPath:@"BranchByList.currency"]);
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[responseStringWithEncoded dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
serverResponse.attributedText = attrStr;
// NSLog(@"%@",attrStr);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
我得到了迴應branch_id = 3但我想顯示爲「貨幣」,但我嘗試了很多,但失敗。
這樣我需要只顯示貨幣.....從服務器 迴應我的迴應:
{"BranchByList":
[
{"id":"342","flag_image":"http:\/\/demo.techzarinfo.com\/newantara\/images\/flags\/USD.png","units":"1","code":"USD B","currency":"US DOLLAR BIG","buy":"4.36","sell":"4.395","updated":"2016-04-11 03:24:24"
},
{"id":"342","flag_image":"http:\/\/demo.techzarinfo.com\/newantara\/images\/flags\/USD.png","units":"1","code":"USD B","currency":"US DOLLAR BIG","buy":"4.36","sell":"4.395","updated":"2016-04-11 03:24:24"
}
]};
1.將您的代碼縮小到相關片段,併爲人們瞭解您的查詢提供更多上下文。 2.爲什麼你使用'NSURLConnection'?它在9.0中被棄用。您現在應該使用'NSURLSession'現在 – NSNoob
顯示您的json響應您想要顯示的內容 – 2016-04-25 04:27:05
將此添加到您的問題的正文中。沒有人會閱讀此評論 – NSNoob