0

My code:

NSString *urlString = [NSString stringWithFormat:@"%@%@%@%@", @"http://api.search.live.net/json.aspx?Appid=am hiding the appid &query=",text,@"&sources=web&web.offset=",offvalue];
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [NSData dataWithContentsOfURL:url];

The JSON response:

SearchResponse =     {
        Errors =         (
                        {
                Code = 1002;
                HelpUrl = "http://msdn.microsoft.com/en-us/library/dd251042.aspx";
                Message = "Parameter has invalid value.";
                Parameter = "SearchRequest.AppId";
                Value = " am hiding the value";
            }
        );
        Query =         {
            SearchTerms = iphone;
        };
        Version = "2.2";
    };
}

What wrong am I doing here.Can anyone please rectify my query??How to use version 2.2 of the bing API

Cœur
  • 37,241
  • 25
  • 195
  • 267
TryingToLearn
  • 365
  • 2
  • 4
  • 15

2 Answers2

0

It looks like you entered the wrong AppID judging from.

Parameter = "SearchRequest.AppId";

Make sure that the AppID matches the appID that they assigned your app with the service.

Also (and this sometimes randomly causes issues for me) make sure you put a / at the end of the URL String.

AdamG
  • 3,718
  • 2
  • 18
  • 28
0

What is the value of 'text', is this one word or multiple words? You'll need to perform URL_Encode on the parameter 'text' in order to have a valid URL.

See Objective-C and Swift URL encoding Be sure only to have the URL_Encoding on the text-object and not on the whole URL, otherwise "http://" will be encoded as well, resulting in an invalid URL as well

Eg a space should be %20 , you can verify this by adding a NSLog of the URL

Community
  • 1
  • 1
Jelle De Laender
  • 577
  • 3
  • 16
  • Text value of depends on the user who types it in the textfield.I am not really getting the concepts of URL_Encoding.please hep me on that. – TryingToLearn Oct 15 '13 at 00:44
  • Let say, the user types "small sentence" , in your case, this will make a URL like http://server.com/api/?para1=small sentence&para2=abc" (eg). A space is not valid in a URL, the valid URL is "http://server.com/api/?para1=small%20sentence&para2=abc" . You can test this by pasting a URL with a space inside the Safari addressbar, Safari will replace the space by %20 etc. You will need to process the input of the user by replacing spaces and other 'strange chars' to the correct %xx values. See http://stackoverflow.com/questions/8086584/objective-c-url-encoding for the solution how to encode – Jelle De Laender Oct 15 '13 at 00:47
  • NSString* encoded_text = [self.encodeText:text]; NSString *urlString = [NSString stringWithFormat:@"%@%@%@%@", @"http://api.search.live.net/json.aspx?Appid=am hiding the appid &query=",encoded_text,@"&sources=web&web.offset=",offvalue]; where self.encodeText: the method is that will handle the encoding of the parameter – Jelle De Laender Oct 15 '13 at 00:48
  • -(NSString*)encodeText:(NSString *)string { NSString *newString = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)string, NULL, CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)); if (newString) { return newString; } return @""; } – TryingToLearn Oct 15 '13 at 00:55
  • you need to implement the method to do the encoding, see http://stackoverflow.com/questions/8086584/objective-c-url-encoding as this describes this problem, and also shows the solution how the encode the parameter. (see approved answer there) – Jelle De Laender Oct 15 '13 at 01:00
  • Also see http://stackoverflow.com/questions/8088473/url-encode-an-nsstring and the approved answer - you can test your method easily with NSLog, spaces should be replaced by %20 etc. – Jelle De Laender Oct 15 '13 at 01:02
  • @rak did you succeed by encoding the text-parameter? Did this fixed the problem you had? If so, can you approve and accept this solution? – Jelle De Laender Oct 15 '13 at 11:25
  • Sure Jelle,thanks for the help.. I have one more question how to send image as binary to the server and in turn it gives me json output – TryingToLearn Oct 15 '13 at 11:45
  • @rak you should open a new question for that, as it's different, but in general, sending binary information will need to be done by making a POST - Request. Please perform some research on POST-Requests. Eg http://stackoverflow.com/questions/7184967/objective-c-post-data-using-nsurlconnection - This question is closed as it was missing url_encode. – Jelle De Laender Oct 15 '13 at 12:08
  • follow this question http://stackoverflow.com/questions/19380980/send-image-as-a-binary-file-to-the-server – TryingToLearn Oct 15 '13 at 12:17