2011-11-24 9 views
6

chciałbym używać tego interfejsu API Google (do testowania tylko):Google'a w Objective-C

https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-US

Moje pytanie brzmi: w jaki sposób należy wysłać żądanie POST do tego adresu URL? Używam:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *recDir = [paths objectAtIndex:0]; 
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/recordTest.flac", recDir]]; 


NSData *myData = [NSData dataWithContentsOfFile:[NSString stringWithFormat:@"%@/recordTest.flac", recDir]]; 
//NSString *audio = [NSString stringWithContentsOfFile:[NSString stringWithFormat:@"%@/recordTest.flac", recDir]]; 



NSMutableURLRequest *request = [[NSMutableURLRequest alloc] 
           initWithURL:[NSURL 
              URLWithString:@"https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-US"]]; 






[request setHTTPMethod:@"POST"]; 

//set headers 

[request addValue:@"Content-Type" forHTTPHeaderField:@"audio/x-flac; rate=16000"]; 

[request addValue:@"audio/x-flac; rate=16000" forHTTPHeaderField:@"Content-Type"]; 

NSString *requestBody = [[NSString alloc] initWithFormat:@"Content=%@", myData]; 

[request setHTTPBody:[requestBody dataUsingEncoding:NSASCIIStringEncoding]]; 

[request setValue:[NSString stringWithFormat:@"%d",[myData length]] forHTTPHeaderField:@"Content-length"]; 



NSHTTPURLResponse* urlResponse = nil; 
NSError *error = [[NSError alloc] init]; 
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error]; 
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 

NSLog(@"The answer is: %@",result); 

Ale mam tylko

{ 
"status":5, 
"id":"fe6ba68a593f9919f5fd33e819d493a0-1", 
"hypotheses":[ 
HERE SHOULD BE THE TEXT 
] 
} 

Co złego/co mam zrobić?

+0

+1, interesujący. –

+0

Btw, niewłaściwie używasz metod 'addValue: forHTTPHeaderField:' i 'setHTTPBody:' - sprawdź ich użycie w dokumentach. –

Odpowiedz

0

Zamiast przeszkadza ze raczej niechlujny realizacji Cocoa, sprawdź mój bardzo łatwy w obsłudze (jedną funkcję) C biblioteki dla Google mowy: http://github.com/H2CO3/libsprec

+0

jest jakiś przykładowy kod dla tego –

+0

@VipinVijay wystarczy odwiedzić link do repozytorium GitHub. Istnieją dwa przykładowe programy. –

+0

Czy możesz podać mi link do tego –

2

FYI cywilny: 0 - prawidłowa, cywilny: 4 - brakujący plik audio, status : 5 - niepoprawny plik audio.

In place of ; 

    NSString *requestBody = [[NSString alloc] initWithFormat:@"Content=%@", myData]; 
    [request setHTTPBody:[requestBody dataUsingEncoding:NSASCIIStringEncoding]]; 

Po prostu użyć;

[request setHTTPBody:myData]; 

Oto Praca kod dla odniesienia:

- (void) googleSTT {

NSString *homeDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 
    NSString *filePath = [NSString stringWithFormat:@"%@/%@", homeDirectory, @"test.flac"]; 

    NSData *myData = [NSData dataWithContentsOfFile:filePath]; 


    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] 
           initWithURL:[NSURL 
              URLWithString:@"https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-US"]]; 

    [request setHTTPMethod:@"POST"]; 

    //set headers 

    [request addValue:@"Content-Type" forHTTPHeaderField:@"audio/x-flac; rate=16000"]; 

    [request addValue:@"audio/x-flac; rate=16000" forHTTPHeaderField:@"Content-Type"]; 

    [request setHTTPBody:myData]; 

    [request setValue:[NSString stringWithFormat:@"%d",[myData length]] forHTTPHeaderField:@"Content-length"]; 

    NSHTTPURLResponse* urlResponse = nil; 
    NSError *error = [[NSError alloc] init]; 
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error]; 
    NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 

    NSLog(@"The answer is: %@",result); 

}

+0

Próbowałem twój kod, ale otrzymuję pusty ciąg wyniku, co zrobiłem jest nagrany plik audio WAV, a następnie przekonwertować go do Plik flac, a teraz za pomocą kodu, ale bez powodzenia, proszę mnie więcej o tym. – Mak13

+0

hej, rozwiązałem to :) dzięki – Mak13

0

Miałem problemy z tego samego problemu, ale potem postanowiłem go przez odnosząc się do tego linku, https://github.com/gillesdemey/google-speech-v2.and po prostuzastąpił twoje żądanie NSMutableURLRequest * tym NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"https://github.com/gillesdemey/google-speech-v2"]];