Formatting an array into JSON - iphone

I am working on an iPhone application. I have to send a POST request to a server in the JSON format. I found the following question: How would I send my array over a HTTP POST to the server?, with good comments suggesting the following URL had a solution: http://andyj.be.s79833.gridserver.com/blog/?p=65
However, this URL now no longer exists. Please provide me any help for posting JSON format data in the form of an associative array or provide any other help.

If you are targeting iOS 5.0, you can use Apple's NSJSONSerialization class to turn an NSArray into a JSON string.

Related

Sending data to a website and getting results of search iOS

I am very new to iOS and I've just begun reading about HTTP requests and POST and GET methods. Let's say, for example, I want to have the user input a string, and then send that data to a website, (for this example, say www.rhymezone.com), search with that string, and get the results of that search within my application. Is this done with an HTTP post method? Or what? Any help / examples would be greatly appreciated. Also, if there are tutorials for this stuff, that would be appreciated as well.
For sake of example, here is what I've tried:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.rhymezone.com/r/rhyme.cgi?Word=test&typeofrhyme=perfect&org1=syl&org2=l&org3=y"]];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSString *dataAsString=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"data: %#",dataAsString);
}
This outputs the entire source of the website (searching for rhymes of the word test). While I can certainly write a method to go through the source of the website and extract the words it returns, I feel like this is not correct. My way of getting rhymes of different words is simply to change the URL here, so where it says 'test' I change it to whatever the user inputs.
Thanks
Look into AFNetworking and RestKit.
It's easiest if you're calling a public API that uses JSON/XML, and then use a built in parser or a parser library to extract the data you want.
Simply downloading the contents of a URL is an HTTP GET request, such as going to a website.
This link talks a bit more about the difference between GET and POST.
When do you use POST and when do you use GET?
If I understand correctly what you are trying to do, I fear that the only option for you is sending the HTTP request (GET or POST according to what the website expects, just like you are doing) and then parse the result to filter all the information that is not relevant.
An alternative approach would be possible if you were using a website offering a REST API, or a JSON API so that you send the query and you get back just the information you need (in a specific format).
So, it depends strongly on the website you are using, but for the generic case, the only option you have is parsing.
(Or, you could display the full content of the page through UIWebView. This would not require explicitly setting up a connection, but I am not sure it is what you are trying to do.)
You are looking for a way to communicate with your website from your iOS application. The common approach is to get the string entered by the user, encode and send it as http request to a sort of script (webservice). This script will do all the stuff you want (search with this string). Then re-send the result to the client (your iOS app) as a http response which will be parsed in your iOS app(with a JSON parser for instance).
There is good resources around that, as an example, you may read this: http://www.raywenderlich.com/2965/how-to-write-an-ios-app-that-uses-a-web-service

Unable to post byte array to webservices (REST) in iOS

I am developing an application which requiers me to convert an audio/video/image file into a 'BYTE ARRAY' and upload that to web services(REST) through a POST request. I am actually using ASIHTTPRequest (ASIFormData) to POST the data. But I could not find any class/method which will allow me to post the byteArray. Is there a way to do this....?
You can post any data with ASIFormDataRequest's setData. Here's an example.
You could create an NSData out of your bytes, and set that to the request using setData:.
NSData Class Reference

Getting information from an online database with iOS

I'm trying to query the ncbi public database. When I look at the network stream, all I can see is that the response header is text/html. Does that mean I have to parse the text/html in iOS to get the information I want in a database? Thanks.
If you are getting data in the form of a HTML document, you will indeed have to parse the HTML to extract the data.

Json Parsing Problem with Dotnet webserivces

I am posting some values to dotnet webservice.For this i am using AsiFormDataRequest.Values are posted suceesfully but coming response is in xml format. so iam not able to get the success or failure message in json format.
What i want to do for getting json response after posting to server.
Thanking in advance.
You need to configure the server to respond with JSON instead of XML. There is nothing you can do about this from within your app. If you don't have control over the server then you need to forget JSON and parse the XML instead.

post xml from iphone to server

How can I post XML data format to server?
(I only know post paramters to server)
Thank you!
It will depend on the server. You could get the NSData -bytes representation of the XML data and, if you're working with a RESTful web application, use PUT or POST when sending the data.