Network Connection Lost - ios

I am facing to
Error - The network connection was lost.
I am using RealmSwift and Alamofire in my project.
This is a real estate app and when ever I am searching properties suddenly I am facing to this error without any exception or error and if I searching again than it works properly.
I don't understand why I am facing this issue.
I am using following code for JSON encoding:
Alamofire
.request(url, method: method, parameters: parameters, encoding: encoding, headers: header)
.responseJSON(completionHandler: { (response) in
Please help.

Are you encoding the parameters as JSON in the request body? If so, encode them as URL:
Alamofire.request(.GET, myURLString, parameters: parameters, encoding: .URL, headers: myHeaders)

Related

Text decoding issue from google translate API in iOS

So here is the URL:
https://translate.googleapis.com/translate_a/single?client=gtx&sl=ar-SA&tl=en-US&dt=t&q=سلام
The response is all fine when is hit from Browser or Postman. But when i do it through code, there are weird strings when getting a response.
I tried Alamofire:
Alamofire.request(urlString, method: .get, parameters: nil,encoding: JSONEncoding.prettyPrinted, headers: ["Accept":"application/json;charset=UTF-8" ]).responseJSON { response in
print(response)
}
The response string is something like this:
"\\U0637\\U00b3\\U0638\\U201e\\U0637\\U00a7\\U0638\\U2026"
and also this weird Arabic string.
"ط ط ط \\\"\\\" ط ... ... ...\"
I also tried hitting URL like this:
String.init(data: Data.init(contentsOf: URL.init(string: "https://translate.googleapis.com/translate_a/single?client=gtx&sl=ar-SA&tl=en-US&dt=t&q=%E2%80%8F%D8%B3%D9%84%D8%A7%D9%85")!), encoding: .utf8)
But the result is the same, some weird Arabic strings, which is different when is hit from browser or postman.
"ط ط ط \\\"\\\" ط ... ... ...\"
I am encoding URL before hitting URL. Also tried different encodings on url and string.
This was the issue with the parameters of API. I added these 2 parameters and it worked all fine.
ie=UTF-8
oe=UTF-8

status code 405 issue..Alamofire POST request...

I am trying to make a Alamofire POST request. This is how I am making the request..
Alamofire.request(url, method: .post, parameters: parameters, encoding: URLEncoding(destination: .queryString), headers : headers)
.responseString { response in
print(response.result)
}
Though I am getting the result as 'SUCCESS', the status code is always shown as 405 while it should have been 200. In the 'encoding' part of the request, I have tried everything like JSONEncoding.default,JSONEncoding.prettyPrinted, URLEncoding.httpbody...but always the status code is still 405. Can anyone please help? Thanks in advance...
This is the solution for this issue...A couple of changes had to be made..
The header which was given was this: let headers = [ "Content-Type" : "application/json"]. But it had to be let headers = [ "Content-Type":"application/x-www-form-urlencoded"].
Also the encoding should be given as URLEncoding.httpBody.
Making these changes made it work fine...
I think problem is with your server because this status code only comes when server disable the api
The HTTP 405 Method Not Allowed response status code indicates that
the request method is known by the server but has been disabled and
cannot be used. The two mandatory methods, GET and HEAD, must never be
disabled and should not return this error code.
So Contact your server(backend developer), make sure your url is correct
I have faced same problem. The API endpoint worked fine with Android Retrofit, also tested with PostMan. Also the header's Content-Type was application/json.
It was really strange bug. I've used Fiddler to check the response.
The error message was
The requested resource does not support http method 'T'/'ST'.
I used GET/POST method, but it said I was using the T/ST, instead of GET/POST
I found answer from the Alamofire's issues.
When I called the API endpoint without parameters, I used the blank dictionary as parameter.
Alamofire.request("URL",
method: .get,
parameters: [:],
encoding: JSONEncoding.default).responseString(completionHandler: completionHandler)
I changed it to nil
Alamofire.request("URL",
method: .get,
parameters: nil,
encoding: JSONEncoding.default).responseString(completionHandler: completionHandler)
After that, it worked fine.
Hope it will help you. Cheers!
Try to replace:
responseString with responseJSON
AND URLEncoding(destination: .queryString) with URLEncoding.default
LIKE:
Alamofire.request(strURL, method: .post, parameters: params, encoding: URLEncoding.default, headers: headers).responseJSON { (responseObject) -> Void in
//Do something
}

Alamofire 4.0 - Set request content type

I'm using Alamofire 4.4.0.
I need to send POST request with params in url string (as in GET request) and with content type "application/x-www-form-urlencoded".
My code is following:
Alamofire.request(url, method: .post, parameters: params, encoding: URLEncoding.default)
.validate()
.responseJSON
Issue is that content type is always "application/json"
I also tried to set encoding to URLEncoding.queryString or .httpBody, also tried to set headers for request as headers: ["Content-Type": "application/x-www-form-urlencoded"]
Please give some advice, how to set request content type in Alamofire 4.0

Fetching http request with query in Alamofire in Swift

I'm trying to fetch data from mLab service from my mongodb database. I can make the request successfully from browser and get data with code below.
https://api.mlab.com/api/1/databases/mysignal/collections/Ctemp?q={"member_id":2}&apiKey=2ABdhQTy1GAWiwfvsKfJyeZVfrHeloQI
I need to change "member_id" to \"member_id\" to not to get sytax error. The rest of is same. However, It doesn't fetch anything with Alamofire in Swift in iOS. (I also tried it without alamofire, with usual http request but still doesn't work)
If I try it without {"member_id":2} It is working.
I'm doing fetching with below code (not working one);
Alamofire.request("https://api.mlab.com/api/1/databases/mysignal/collections/Ctemp?q={\"member_id\":2}&apiKey=2ABdhQTy1GAWiwfvsKfJyeZVfrHeloQI")
I also try to add parameters
let parameters: Parameters = ["member_id": "3"]
Alamofire.request("https://api.mlab.com/api/1/databases/mysignal/collections/Ctemp?q={\"member_id\":3}&apiKey=2ABdhQTy1GAWiwfvsKfJyeZVfrHeloQI", parameters: parameters, encoding: URLEncoding(destination: .methodDependent))
This is the api document;
http://docs.mlab.com/data-api/
Thank You
Your request should look like that:
let parameters: Parameters = [
"q": ["member_id": 2],
"apiKey": "2ABdhQTy1GAWiwfvsKfJyeZVfrHeloQI"
]
Alamofire.request("https://api.mlab.com/api/1/databases/mysignal/collections/Ctemp", method: .get, parameters: parameters, encoding: URLEncoding.default)
It is more readable, and also you can easily change the parameters.
Hope it helps

Alamofire 4 network stream closed

I am trying to make a POST request using latest Alamofire 4.0.0 / Swift 3 passing JSON in the request.
The request calls a wildfly server and takes up to a minute to return with data.
However, I constantly get a server error java.io.IOException: UT010029: Stream is closed.
let headers: HTTPHeaders = [
"Content-Type": "application/json",
"Accept": "application/json",
"Connection": "Keep-Alive"
]
Alamofire.request("myUrl", method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers)
.validate().responseJSON { response in
print("Response")
print(response)
}
}
Alamofire doesn't complain at all and doesn't log anything.
I have tried other requests to the same server/service that return instantly and I get into the completion handler.
Is this just a timeout issue? If so how can I adjust it?
I was getting a very similar problem with GET requests. Turns out I was passing an empty dictionary [:] as parameters to the request method. Omitting the parameters: completely in the call solved the issue.

Resources