I am creating a app in which I want to fetch data from PhpMyAdmin database to my UITableView.
Is there any good tutorial for this purpose? Please not any chat app tutorials I simply want to display my table in tableview.
My table has 5 fields (id, fname, lname, email, address).
Thank you
The best way to handle it is to create a JSON API for your database and then make your iOS app parse the JSON data.
Here is an example of how to create a JSON output from MySQL using PHP.
When you have your JSON output you should download it from the app using the NSURLConnectionand NSJSONSerialization, something like this:
NSURL* url = [NSURL urlWithString:#"http://yourapi.url/here"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLResponse *response = nil;
NSError **error = nil;
NSData *data = [[NSData alloc] initWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:error]];
NSInteger httpStatus = [((NSHTTPURLResponse *)response) statusCode];
NSLog(#"responsecode:%d", httpStatus);
NSDictionary *parsedJSON = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&errorInfo];
The content of your JSON will be placed in the parsedJson dictionary.
This is just a quick example of using a synchronous request with NSURLConnection. If you want a asynchronous solution you should read the documentation on NSURLConnection.
To show the data in a UITableView you should checkout this tutorial.
Related
i want to take 450
from the following:
NSString {status:0,val:450}
using objective c:
{status:0,val:450}
Please suggest me answer
I cannot completely understand your question . I think you have you have an JSON data like:
{
status:0;
val:450;
}
If you want this data in your app. You want to do
NSURL *blogURL = [NSURL URLWithString:#"https://your url here"];
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSDictionary *data = [dataDictionary objectForKey:#"status"];
NSDictionary *item = [data objectForKey:#"val"];
Your JSON Data get in Dictionary format. You can access the Data using the Key. In here your keys are status and val.
I hope this links are help for you.
fetch parse json ios programming tutorial
and this Link:
json parsing in ios
I want to get a MP3 link from a website, I don't know exactly what type of this website is, but it only has content like this (link: http://www.nhaccuatui.com/download/song/4Upyxq0QlytX)
{"error_message":"Success","data":{"stream_url":"http:\/\/download.s81.stream.nixcdn.com\/dd634cb8afcc15d7c17a8ce4c548709f\/533cc58e\/NhacCuaTui791\/KhongQuanTam-ChiDan_4cyw4_hq.mp3","is_charge":"false"},"error_code":0,"STATUS_READ_MODE":true}
What can I do if I want to get content (link mp3) from key (?) "stream_url" from this link to put it in to my iOS Applications?
If you rearrange your JSON it will look like this:
{
"error_message":"Success",
"data":{
"stream_url":"http:\/\/download.s81.stream.nixcdn.com\/dd634cb8afcc15d7c17a8ce4c548709f\/533cc58e\/NhacCuaTui791\/KhongQuanTam-ChiDan_4cyw4_hq.mp3",
"is_charge":"false"
},
"error_code":0,
"STATUS_READ_MODE":true
}
From that you can see its a dictionary.
If you want to get it via url request do something like this:
NSDictionary *dictionaryData;
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.00];
NSHTTPURLResponse *response;
NSError *error = [[NSError alloc]init];
NSData *apiData = [NSURLConnection sendSynchronousRequest:requestURL returningResponse:&response error:&error];
dictionaryData = [NSJSONSerialization JSONObjectWithData:apiData options:kNilOptions error:&error];
Then you can get the url by doing this:
NSString *str = [[dictionaryData objectForKey:#"data"] objectForKey:#"stream_url"];
Then make another request for getting the file.
The data you presented is in JSON format. To access it, use NSJSONSerialization.
I am trying to store data from server to NSMutable array to display them as news feeds in table view like shown in this image. Basically like twitter news feeds. What I wanna do is get the data from the server in the NSMutable array and use that array to display in my table view. I don't know if this is the right way to do it. I tried adding statically and it works but I really don't know how to do it dynamically since I'm a newbie to Objective C. Sorry if this question seems really stupid. Thanks in advance!
Parse data using JSON:
dispatch_queue_t jsonParsingQueue = dispatch_queue_create("jsonParsingQueue", NULL);
// execute a task on that queue asynchronously
dispatch_async(jsonParsingQueue, ^{
NSString *urlStr = #"YourURL";
NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
[request setHTTPMethod: #"GET"];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *responseStr = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSData * jsonData = [responseStr dataUsingEncoding:NSUTF8StringEncoding];
NSMutableArray *tempResults = [NSMutableArray alloc];
NSError *jsonParsingError = nil;
NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&jsonParsingError];
tempResults = jsonObject[#"posts"]; //Add the json key you would like to get
self.arrayToDisplay = [tempResults copy]; //copy them to your NSMutableArray
// some code on a main thread (delegates, notifications, UI updates...)
dispatch_async(dispatch_get_main_queue(), ^{
[self.myTableView reloadData];
});
});
I am looking to use JSON data to create a route using the Google Maps API and extract JSON data that will be displayed onto a text field that includes "distance" and "duration". I would like to have to text fields that will reverse geocode and send a request to the Google Map API. Here is an example code I'm using:
// Create new SBJSON parser object
SBJSON *parser = [[SBJSON alloc] init];
// Prepare URL request to download statuses from Twitter
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://twitter.com/statuses/public_timeline.json"]];
// Perform request and get JSON back as a NSData object
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
// Get JSON as a NSString from NSData response
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
// parse the JSON response into an object
// Here we're using NSArray since we're parsing an array of JSON status objects
NSArray *statuses = [parser objectWithString:json_string error:nil];
// Each element in statuses is a single status
// represented as a NSDictionary
for (NSDictionary *status in statuses) {
// You can retrieve individual values using objectForKey on the status NSDictionary
// This will print the tweet and username to the console
NSLog(#"%# - %#", [status objectForKey:#"text"], [[status objectForKey:#"user"] objectForKey:#"screen_name"]);
}
Rather than it print onto the screen I would like for this to print onto a text field while gathering specific data from the JSON data. I would also like to be able to reverse geocode.
I am creating a simple Login page. In my project I want to parse the json string. But it gives me following error.
-JSONValue failed. Error trace is: (
"Error Domain=org.brautaset.JSON.ErrorDomain Code=4
\"Valid fragment, but not JSON\"
UserInfo=0xa6e70a0 {NSLocalizedDescription=Valid fragment, but not JSON}"
In my code if I am putting another json string than it is working. And also my original json string is giving me the data in browser. So what to do?
My Code is:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://dev.bevbucks.com/gbs/api.json/token?user=rverma#prismetric.com&pwd=verma!pris"]];
NSURLResponse *response = nil;
NSError *error = nil;
//getting the data
NSData *newData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
//json parse
NSString *responseString = [[NSString alloc] initWithData:newData encoding:NSUTF8StringEncoding];
NSDictionary *jsonObject = [responseString JSONValue];
NSLog(#"type : %#", jsonObject );
The returned string:
"60ee094456b6fc03f386af50c443b471"
Isn't valid JSON, and should at least be:
[ "60ee094456b6fc03f386af50c443b471" ]
So there is a bug in the server, and not your code. If you cannot get this bug fixed then you're going to have to workaround it.
From JSON.org:
JSON is built on two structures:
A collection of name/value pairs. In various languages, this is
realized as an object, record, struct, dictionary, hash table, keyed
list, or associative array.
An ordered list of values. In most
languages, this is realized as an array, vector, list, or sequence.