0

HTTP GET および POST リクエスト/レスポンスに取り組んでいます。POSTでは問題なく動作していますが、GETメソッドでは行き詰まっています..

私のコンソールでは、379 バイトのデータを取得しています。しかし、コンソールにデータ全体 (機械可読形式) を表示する必要があります。その後、JSON 形式に変換します。

コメント行は POST メソッドです。

これが私のコードです..

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    NSString *post = [NSString stringWithFormat:@"&Username=%@&Password=%@",@"username", @"password"];
//    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
//    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.abcde.com/xyz/login.aspx"]]];
    [request setHTTPMethod:@"GET"];
//    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type" ];
//    [request setHTTPBody:postData];
    NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];

    if(conn)
    {
        NSLog(@"Connection Successful");
    }
    else
    {
        NSLog(@"Connection Not Succeeded");
    }

}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert 1" message:@"Data Received" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:Nil, nil];
    [alert show];

}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert 2" message:@"Error Occurred" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:Nil, nil];
    [alert show];
    NSLog(@"Error Description is here: %@", error.description);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert 3" message:@"Finished Loading" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:Nil, nil];
    [alert show];
//    NSLog(@"Displaying the Datas Received %@",);

}

あなたの助けは非常に高く評価されています。前もって感謝します。

4

4 に答える 4

2
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"Displaying the Datas Received %@",data);
    NSString *strResult = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"Displaying the Datas Received In Readable Format %@",strResult);
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert 1" message:@"Data Received" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:Nil, nil];
    [alert show];

}
于 2013-11-06T07:00:49.137 に答える
0

データが長すぎる場合は、データをチャンクで受信し、デリゲートdidRecieveData:が自動的に複数回呼び出されます。そのため、応答で受け取ったすべてのデータを に追加する必要がありますNSMutableData。オブジェクトを作成し、デリゲートNSMutableDataでデータを受け取るときにデータを追加します。didRecieveData:

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [myData appendData:data];
}

最後に、リクエストが完了したら、そのデータを文字列に変換します。

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    NSString * stringData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert 3" message:@"Finished Loading" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:Nil, nil];
    [alert show];
    NSLog(@"Displaying the Datas Received %@", stringData);

}
于 2013-11-06T08:14:46.030 に答える
0

URL リクエストのデリゲート メソッドを終了する必要があると思います。コードは次のようになります

@interface YourClass : UIViewController <NSURLConnectionDataDelegate> {
        NSMutableData *__downloadingData;
}   

//YourClass Delegation Handlers
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{

    if (!__downloadingData)
        __downloadingData = [NSMutableData dataWithCapacity:0];

    [__downloadingData appendData:data];
    //
//        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert 1" message:@"Data Received" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:Nil, nil];
//    [alert show];

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    NSLog(@"Data Received %@",__downloadingData);
    NSString *strResult = [[NSString alloc] initWithData:__downloadingData encoding:NSUTF8StringEncoding];
    NSLog(@"Data Received In Readable Format %@",strResult);


}
于 2013-11-06T08:22:28.140 に答える