json


Requested URL not found [duplicate]


I was trying to download a picture to my app from
The request failed with the error NSURLErrorDomain and the code is really -1100. The url should be correct since I checked it in the browser. Anyone knows why?
let userImageURL: String! = "http://i.imgur.com/QhCzQoR.jpg";
let url = NSURL(fileURLWithPath: userImageURL);
let request:NSURLRequest = NSURLRequest(URL: url!)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: { (response:NSURLResponse!, imageData:NSData!, error:NSError!) -> Void in
let image = UIImage(data: imageData!);
})
I code in Objective-C, it should be easy to convert it in Swift (small modification in fact, but the explanation is not that much related to code).
If you check the error complete message, you'll get:
Error Domain=NSURLErrorDomain Code=-1100 "The requested URL was not
found on this server." UserInfo=0x7997b7e0
{NSErrorFailingURLStringKey=file:///http:/i.imgur.com/QhCzQoR.jpg,
NSErrorFailingURLKey=file:///http:/i.imgur.com/QhCzQoR.jpg,
NSLocalizedDescription=The requested URL was not found on this
server., NSUnderlyingError=0x799f3080 "The requested URL was not found
on this server."}
Clearly, if you look carefully at the URL, you have file:///http:/i.imgur.com/QhCzQoR.jpg, which is not the URL wanted. If you log url.absoluteString, you'll see it to your URL, it's you that set it like this.
Why? Because you used fileURLWithPath: instead of URLWithString:.
So change your line with:
let url = NSURL(URLWithString: userImageURL);
Some discussions about the differences between theses two:
What is difference between URLWithString and fileURLWithPath of NSURL?
or the doc.
The reason you are getting this problem is because you have used
let url = NSURL(fileURLWithPath: userImageURL);
Instead you should use:
let url = NSURL(string: userImageUrl)

Related Links

Seam 2.3 RESTEasy on JBoss 7.1 #JsonIgnore
Gathering information from a textfile. Corona SDK
calling rest api in spring
Upload homewizard data to cosm
encoding/json unmarshal missing a field
Play Framework 2.1.1 Json Writes nested objects
Converting nested lists to json gives superfluous arrays
REST design advice on simple order service
Chrome Manifest.json syntax error
Importing JSON data in Highcharts
Uncaught SyntaxError : Unexpected string (line 5)
Handle multidimensional JSON with scala Play framework
Elasticsearch Delete Mapping Property
JQuery AJAX and autopopulate dropdown menu
ListView and Data Models - Blackberry
ng-repeat not accurately pulling data

Categories

HOME
sidekiq
textwatcher
ubuntu-16.04
extract
concourse
search-engine
javamail
height
android-viewpager
maxima
phoenix
rapidjson
ssms-2016
google-openid
aptana
cython
tortoisegit
google-maps-android-api-2
memsql
bootstrap-typeahead
philips-hue
skmaps
frp
piecewise
ipfw
freebase
getline
powershell-remoting
codesys
magiczoomplus
fltk
pdftk
spring-bean
lftp
powermta
cppunit
fastq
rapidweaver
data-integration
seamless-immutable
webdatagrid
spell-checking
pluck
crash-reports
info
httplib2
infinite-scroll
addin-express
visual-studio-templates
sqldatareader
pdflib
typhoon
tomee
audioqueue
gitweb
angularjs-ng-pattern
freetype2
amf
soda
image-editing
kendo-combobox
search-regex
loopj
anythingslider
data-import
affix
p-np
futuretask
apigee-baas
base32
playscape
harp
gray-code
virus
word-2013
pl-i
moai
libsndfile
yahoo-boss-api
funkload
map-force
lov
circos
real-time-updates
vertical-rhythm
nimrod
dynamic-binding
vmware-server
backbone-relational
magickwand
domain-calculus
transitive-closure-table
cgpath
rose-db-object
applicationcontext
objective-c-protocol
supersized
driving-directions
zookeeper
electronic-signature
scraperwiki
windows-live-id
ets
hadoop-plugins
boost-date-time
idictionary
oggvorbis
plinq
callgrind
odbc-sql-server-driver
usergroups

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App