I have an iOS application (Swift) which encodes some data and does a JSONSerialization and creates a JSON object. My code is below, I've done my best to keep it tidy, so I hope it makes sense:
struct Order: Codable {
let idQty: [FoodIdAndQuantity]
let collection: String
let name: String
let phone: Int
let doorNum: Int
let street: String
let postcode: String
}
struct FoodIdAndQuantity: Codable {
let itemId: Int
let qty: Int
}
class CheckoutServer: NSObject, URLSessionDataDelegate {
var inputValuesForItemAndQuantity = [Int:Int]()
var idQty = [FoodIdAndQuantity]()
var collection = String()
var name = String()
var phone = Int()
var doorNum = Int()
var street = String()
var postcode = String()
var request = URLRequest(url: NSURL(string: "http://192.168.1.100/api/AddOrder.php")! as URL)
func sendToDatabase() {
for(key,value) in inputValuesForItemAndQuantity {
idQty.append(FoodIdAndQuantity(itemId: key, qty: value))
}
let order = Order(idQty: idQty,collection: collection,name: name,phone: phone,doorNum: doorNum,street: street,postcode: postcode)
let encodedOrder = try? JSONEncoder().encode(order)
var json: Any?
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
if let data = encodedOrder {
json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments)
if var json = json {
if JSONSerialization.isValidJSONObject(json) {
do {
json = try JSONSerialization.data(withJSONObject: json, options: .prettyPrinted)
} catch {
print("There was a problem creating the JSON object")
}
} else {
print("not valid JSON")
}
}
}
let postParameters = "json="+String(describing: json!)
print(String(describing: json!)) //Print JSON for debugging purposes
request.httpBody = postParameters.data(using: .utf8)
let defaultSession = URLSession(configuration: URLSessionConfiguration.default)
let task = defaultSession.dataTask(with: request) { (data, response, error) in
if error != nil {
print("Failed to download data at Menu Type Items")
} else {
print("Data uploaded")
}
}
task.resume()
}
}
So the above code does the following:
Creates an encodable object called 'order'.
Creates a POST request to my API.
Passes the encoded JSON object via a POST parameter.
I've printed the json object that gets posted back to the console in XCode and that looks as follows:
{
collection = Delivery;
doorNum = 99;
idQty = (
{
itemId = 17;
qty = 5;
},
{
itemId = 1;
qty = 3;
}
);
name = James;
phone = 012345667;
postcode = LXU49RT;
street = Hope Street;
}
Next, I'll move over to my server/API which accepts the POST parameter.
Below is my AddOrder.php page:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
require_once dirname(__FILE__) . '/DbOperation.php';
$json = $_POST["json"];
$db = new DbOperation();
$json = $db->addOrder($json);
}
And below is my DbOperation addOrder function:
public function addOrder($json) {
require dirname(__FILE__) . '/../../dbconnect.php';
$decoded = json_decode($json);
$collection = $decoded{"collection"};
$stmt2 = $pdo->prepare("INSERT INTO TestTable (collection) VALUES (:collection)");
$stmt2->bindParam(':collection',$collection);
$stmt2->execute();
}
It's worth noting that, whilst I try to fix this issue, I have created a test table in my Database which simply stores the collection element of the JSON.
The problem I have is, when I run my application and send the data, nothing gets stored in the database, and my apache error.log file says the Column 'collection' cannot be null. So I assume I am handling the POST parameter incorrectly at some point of my PHP. Unless the fault lies at a Swift level, which I'll add the Swift tag to this post if asked by an admin.
The full error is below:
[Wed Feb 28 15:44:55.178184 2018] [:error] [pid 520] [client 192.168.1.46:52400] PHP Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'collection' cannot be null in /var/www/api/DbOperation.php:111\nStack trace:\n#0 /var/www/api/DbOperation.php(111): PDOStatement->execute()\n#1 /var/www/api/AddOrder.php(16): DbOperation->addOrder(NULL)\n#2 {main}\n thrown in /var/www/api/DbOperation.php on line 111
What I've tried
I've tried altering my AddOrder.php page to the following:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
require_once dirname(__FILE__) . '/DbOperation.php';
//$json = $_POST["json"];
$json = json_decode(file_get_contents('php://input'),true);
$db = new DbOperation();
$json = $db->addOrder($json);
}
Your swift code doesn't make much sense. You have code that uses a JSONEncoder to encode your swift object into Data. If that succeeds, you then convert the data back to a Swift object using JSONSerialization. if that succeeds, you then use JSONSerialization.data(withJSONObject:options:) to convert your Swift object back to JSON data, and then use String(describing:) your insanely over-processed JSON Data to a string, which is very, very wrong.
Get rid of all that code. Try this instead:
func sendToDatabase() {
for(key,value) in inputValuesForItemAndQuantity {
idQty.append(FoodIdAndQuantity(itemId: key, qty: value))
}
let order = Order(idQty: idQty,collection: collection,name: name,phone: phone,doorNum: doorNum,street: street,postcode: postcode)
guard let encodedOrder = try? JSONEncoder().encode(order) else { return }
request.httpBody = encodedOrder
let defaultSession = URLSession(configuration: URLSessionConfiguration.default)
let task = defaultSession.dataTask(with: request) { (data, response, error) in
if error != nil {
print("Failed to download data at Menu Type Items")
} else {
print("Data uploaded")
}
}
task.resume()
}
The following is not valid code, and should throw a fatal "Cannot use object of type stdClass as array":
$decoded = json_decode($json);
$collection = $decoded{"collection"};
You probably want this:
$decoded = json_decode($json, true);
$collection = $decoded["collection"];
Or this:
$decoded = json_decode($json);
$collection = $decoded->collection;
I noticed a json decoding issue with incoming json from Swift 5 to php. Using file_put_contents in my php API, I found the json string from Swift looks like this
[{"Description":"box 3 of 3","Name":"Box S-7","Barcode":"1007","ComponentID":"50","Notes":"ok"}]
which if you convert with json_decode will throw an error, unless you remove the [ and ] from beginning and end of the string, respectively
$new_json = substr($json, 1, -1);
$decode = json_decode($new_json);
Related
I have an app in xcode where I will be taking notes and uploading them to a server on the web. I have the following code in xcode to POST. I'm getting an error
The data couldn’t be read because it isn’t in the correct format.
Here are my parameters to upload to php
let parameters: [String: Any] = ["title": title, "post": post]
Here is my create Post code
func createPost(parameters: [String: Any]) {
guard let url = URL(string: "\(prefixUrl)/post.php") else {
print("Did not find url")
return
}
let data = try! JSONSerialization.data(withJSONObject: parameters)
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = data
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
URLSession.shared.dataTask(with: request ) { (data, res, error) in
if error != nil {
print("error", error?.localizedDescription ?? "")
return
}
do {
if let data = data {
let result = try JSONDecoder().decode([postDataBase].self, from: data)
DispatchQueue.main.sync {
print(result)
}
}else {
print("No Data")
}
} catch let JsonError {
print("fetch json error:", JsonError.localizedDescription)
}
}.resume()
}
The create post is wrapped in a class ViewDataBase with other CRUD commands...
I have a struct postDataBase where i know the error The data couldn’t be read because it isn’t in the correct format. could be happening as well. I've changed these to various data types with no success
struct postDataBase: Decodable {
var ID: String
var title: String
var post: String
}
My php code is here. I use bluehost to support my database so i can have a public domain. If you need my username and password let me know i can change it later.
<?php
$connection=mysqli_connect("localhost","****","****");
$ID = $_POST['ID'];
$title = $_POST['title'];
$post = $_POST['post'];
if(!$connection)
{
die('Connection Failed');
}
else
{
$dbconnect = #mysqli_select_db('mlbroadv_PlantAssistDB', $connection);
if(!$dbconnect)
{
die('Could not connect to Database');
}
else
{
$query = "INSERT INTO Notes (title, post) VALUES ( '$title', '$post');";
mysqli_query($query, $connection) or die(mysqli_error());
echo 'Successfully added.';
echo $query;
}
}
?>
My goal is getting my swift data uploaded to php and into mySQL database. There seems to be a disconnect somewhere...
I think the error lies in the following code where the php files is read in to connect with mySQL and to upload my variables...
EDIT:
let data = try! JSONSerialization.data(withJSONObject: parameters)
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = data
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
URLSession.shared.dataTask(with: request ) { (data, res, error) in
EDIT 2: Still Trying to get it
For this line...
let parameters: [String: Any] = ["title": "Fgh", "post": "Fgg"
I get...
Param raw data is coded as: ["post": "Fgg", "title": "Fgh"]
For this line...
let dataParam = try! JSONSerialization.data(withJSONObject: parameters)
I Get...
json serialization request is coded as: 28 bytes
For this line...
URLSession.shared.dataTask(with: request as URLRequest ) { (data, res, error) in
if error != nil {
print("urlsession error is coded as: ", error?.localizedDescription ?? "")
return
}
I Get for the data varaible...
urlsession data is coded as: 235 bytes
and for the res variable, I get the following repsonse...
urlsession results is coded as: <NSHTTPURLResponse: 0x282ea64c0> { URL: https://mlbroadvisions.com/post.php } { Status Code: 200, Headers {
"Content-Encoding" = (
gzip
);
"Content-Type" = (
"text/html; charset=UTF-8"
);
Date = (
"Fri, 22 Jul 2022 16:07:03 GMT"
);
Server = (
cloudflare
);
Vary = (
"Accept-Encoding"
);
"cf-cache-status" = (
DYNAMIC
);
"cf-ray" = (
"72ed6d53f9072be1-ORD"
);
"expect-ct" = (
"max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\""
);
"host-header" = (
"c2hhcmVkLmJsdWVob3N0LmNvbQ=="
);
} }
during the ...
catch let error as NSError {
print(error.localizedDescription)
It gives me the error...
The data couldn’t be read because it isn’t in the correct format.
json data is coded as: 235 bytes
What part of my code do i need to change to get it into the correct format i've tried a lot of things none of which worked.
I have this issue when trying to read my data which is json encoded from the php page to the swift page.
this is the code I am using
import Foundation
protocol HomeModelProtocol: class {
func itemsDownloaded(items: NSArray)
}
class HomeModel: NSObject, URLSessionDataDelegate {
//properties
weak var delegate: HomeModelProtocol!
var data = Data()
let urlPath: String = "http://localhost/service.php" //this will be changed to the path where service.php lives
func downloadItems() {
let url: URL = URL(string: urlPath)!
let defaultSession = Foundation.URLSession(configuration: URLSessionConfiguration.default)
let task = defaultSession.dataTask(with: url) { (data, response, error) in
if error != nil {
print("Failed to download data")
}else {
print("Data downloaded") // this work fine
self.parseJSON(data!)
}
}
task.resume()
}
func parseJSON(_ data:Data) {
var jsonResult = NSArray()
print(jsonResult) // this print empty parentheses
print(String(data: data, encoding: .utf8)) // this prints out the array
//the code below throughs an arror
do{
jsonResult = try JSONSerialization.jsonObject(with:data, options:JSONSerialization.ReadingOptions.allowFragments) as! [NSArray] as NSArray
print(jsonResult)
} catch let error as NSError {
print(error)
}
var jsonElement = NSDictionary()
let locations = NSMutableArray()
for i in 0 ..< jsonResult.count
{
jsonElement = jsonResult[i] as! NSDictionary
let location = LocationModel()
//the following insures none of the JsonElement values are nil through optional binding
if let name = jsonElement["Name"] as? String,
let address = jsonElement["Address"] as? String,
let latitude = jsonElement["Latitude"] as? String,
let longitude = jsonElement["Longitude"] as? String
{
location.name = name
location.address = address
location.latitude = latitude
location.longitude = longitude
}
locations.add(location)
}
DispatchQueue.main.async(execute: { () -> Void in
self.delegate.itemsDownloaded(items: locations)
})
}
}
this is the output which I am receiving:
Data downloaded
(
)
Optional(" \nconnectedinside[{\"name\":\"One\",\"add\":\"One\",\"lat\":\"1\",\"long\":\"1\"},{\"name\":\"Two\",\"add\":\"Two\",\"lat\":\"2\",\"long\":\"2\"},{\"name\":\"One\",\"add\":\"One\",\"lat\":\"1\",\"long\":\"1\"},{\"name\":\"Two\",\"add\":\"Two\",\"lat\":\"2\",\"long\":\"2\"}]")
Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around
character 2." UserInfo={NSDebugDescription=Invalid value around
character 2.}
You get this error, because the json response you receive is not an array but a dictionary.
EDIT: as pointed out in a comment, you first need to fix your json response in your php code. There is ":" missing after "connectedinside".
It should look like this:
{\"connectedinside\":[{\"name\":\"One\",\"add\":"One",...},...]}
My suggestion to fix this:
You should have two models:
struct HomeModelResponse: Codable {
let connectedinside: [LocationModel]
}
// your LocationModel should look like this:
struct LocationModel: Codable {
let name: String
let add: String
let lat: String
let long: String
}
And change your JSONDecoding code to:
do {
jsonResult = try? JSONDecoder().decode(HomeModelResponse.self, from: data)
print()
} catch let exception {
print("received exception while decoding: \(exception)"
}
Then you can access your LocationModels by jsonResult.connectedinside
The problem was on my php side and I fixed it.it is working now.
I declared a function to send information from some textfields (POST), then I get a response with the information given. I also used components(separatedBy: String) to get an array with the information from every field. What am trying to do now is to use this information (one from the array) to store/use it so I can show the user, in a new view, the information saved.
My Swift looks like the following
#IBAction func enviarInfo(_ sender: Any) {
let request = NSMutableURLRequest(url: NSURL(string: "http://www.mydomain/index.php")! as URL)
request.httpMethod = "POST"
//The String with the vars that will be sent to the $_POST["var"]
let postString = "nombre=\(nombreText.text!)&aPaterno=\(apaternoText.text!)&aMaterno=\(amaternoText.text!)&genero=\(genero.text! &email=\(emailText.text!)&telefono=\(telefonoText.text!)"
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if error != nil {
print("error=\(String(describing: error))")
return
}
print("response = \(String(describing: response))")
let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print("responseString = \(String(describing: responseString))")
//Use a split method to get relevant information only
let string: String = responseString! as String
var str = string.components(separatedBy: "##")
let resultadoUno = str[1]
var str2 = resultadoUno.components(separatedBy: ">>")
let resultadoDos = str2[0]
var str3 = resultadoDos.components(separatedBy: ";")
}
task.resume()
}
If I print, for example, str3[5] I get the phone number, or a name with str3[0], but what I don't know is, how to save the data and show it to the user via labels (label.text = the value from the array).
You can easily save Response string to UserDefaults, and restore and use them later.
First, define a class level constant as UserDefaults key:
let key = "responseString"
Second, save the string:
UserDefaults.standard.set(responseString, forKey: key)
UserDefaults.standard.synchronize()
Restore and use it when you need it:
guard let repsonseString = UserDefaults.standard.object(forKey: key) as? String else {
return
}
var strArray = repsonseString.components(separatedBy: "##")
if strArray.count > 0 {
let str2 = strArray[0].components(separatedBy: ">>")
// do something with str2
}
if strArray.count > 1 {
let str3 = strArray[1].components(separatedBy: ";")
if str3.count > 5 {
self.phoneLabel.text = str3[5]
}
}
The better practice is the PHP returns the dictionary instead of array, like:
{
phoneNumber:123456789,
userName:"Joy"
}
You get the response on iOS and convert JSON dictionary to Swift dictionary, and parse it into a Model(Like a User class). Then it will be very convenient to access data like: self.phoneNumberLabel.text = user.phoneNumber
I have a UIPickerview which represents countries and i am trying to get its data from MYSQL database but i couldnt handle the coming data from PHP file. I couldnt find any Swift solution so thats why i am here.
//its the default values of pickerview
var pickOption = ["one", "two", "three", "seven", "fifteen"]
func getCountries() {
let url:NSURL = NSURL(string: "http:/domain.com/getCountriesLong.php")!
let session = NSURLSession.sharedSession()
let request = NSMutableURLRequest(URL: url)
let task = session.downloadTaskWithRequest(request) {
(
let location, let response, let error) in
guard let _:NSURL = location, let _:NSURLResponse = response where error == nil else {
print("error")
return
}
let urlContents = try! NSString(contentsOfURL: location!, encoding: NSUTF8StringEncoding)
guard let _:NSString = urlContents else {
print("error")
return
}
//all result is here
//print(urlContents)
//string to NSData conversion
let data = urlContents.dataUsingEncoding(NSUTF8StringEncoding)
do {
//parse NSData to JSON
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
//pickOption = json["countryName"]
//pickOption = json
} catch {
print("error serializing JSON: \(error)")
}
}
task.resume()
}
Here is the PHP
$menuData = $db->get_results("SELECT countryName FROM countries ORDER BY countryName ASC");
echo json_encode($menuData);
How can i use coming data as value of my UIPickerView ?
I found the solution. instead of using pickOption = json["countryName"] i have declared new array. Here it is;
var arr = [String]()
for name in json as! [AnyObject] {
if let country = name["countryName"] as? String {
arr.append(country)
}
}
self.pickOption = arr
I have been struggling to get a web service to work with a native Swift application when a user is attempting to upload data using the "&" character, amongst others.
After various attempts at escaping the request, I am at a loss and looking for some advice to solving this. I have included a some code from both the Swift application and the PHP scripts for reference. The data is able to be posted to the server (I am using FileMaker Server PHP API), but not when the "&" is included in a user-submitted value as the JSON is being cut short when PHP hits that character.
The postData argument in the Swift request sample is a dictionary in JSON format, which is encoded using the below code from JSONStringify:
func JSONStringify(value: AnyObject, prettyPrinted: Bool = false) -> String {
let options = NSJSONWritingOptions.PrettyPrinted
if NSJSONSerialization.isValidJSONObject(value) {
do {
let data = try NSJSONSerialization.dataWithJSONObject(value, options: options)
if let string = NSString(data: data, encoding: NSUTF8StringEncoding) {
return string as String
}
} catch {
return ""
}
}
return ""
}
Swift Request
class func returnAnyObject (phpFile:String, postData:String) -> AnyObject? {
let server:String = "http://myserver.com"
let url = NSURL(string: "\(server)\(phpFile)")
let cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
let request = NSMutableURLRequest(URL: url!, cachePolicy: cachePolicy, timeoutInterval: 10.0)
request.HTTPMethod = "POST"
// set data
let dataString = postData.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
let requestBodyData = (dataString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
request.HTTPBody = requestBodyData
var response: NSURLResponse? = nil
do {
let reply: NSData = try NSURLConnection.sendSynchronousRequest(request, returningResponse:&response)
if let results = NSString(data:reply, encoding:NSUTF8StringEncoding) {
return results
} else {
return nil
}
} catch {
return nil
}
}
PHP
Below is the relevant portion of the PHP script. Perhaps there is some step I am missing with this as well?
<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE);
$req1 = '../../FileMaker.php';
$req2 = '../../FM_Connect.php';
require_once $req1;
require_once $req2;
//set fixed variables
$layout = 'php_Lineitems';
//Define passed variables
$json = $_POST['json'];
$json = stripslashes($json);
$fieldArray = json_decode($json); // This is now an associative array
?>
Don't put the JSON as a string, you don't need to stringify it, should be of NSData type.
Then you set that in the body of your request directly.
Note on the PHP side you won't be able to access $_POST['json'].
You will need to change your code to:
$json = json_decode(file_get_contents('php://input'));