The images are stored at an server requiring authorization. Is there any header value that we missed here or could you ust help me resolve this issue or point out to the right direction.
SDWebImageDownloader * downloader = [SDWebImageDownloader sharedDownloader];
[downloader setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[downloader setValue:#"image/png" forHTTPHeaderField:#"Content-Type"];
[downloader setValue:[self getAuthorizationHeaderValue] forHTTPHeaderField:#"Authorization"];
[downloader setValue:#"UTF-8" forHTTPHeaderField:#"Encoding"];
[downloader downloadImageWithURL:[NSURL URLWithString:imageUrl] options:SDWebImageDownloaderHighPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) {
NSLog(#"\n Progress download ......");
} completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
if (image && finished) {
NSLog(#"Success downloading image, returning image........");
[subscriber sendNext:image];
[subscriber sendCompleted];
}
else if (error && finished) {
NSLog(#"Error downloading image, returning placeholder image........");
[subscriber sendError:error];
[subscriber sendCompleted];
}
}];
The image download fails with error 404 means your URL image is not exist on the specified server path. You can double check, by trying open that URL in your computer's browser, beware, even if it'll showing you an image for that bad URL, clear your cache of your browser and then reload that URL.
Related
I created an NSDictionary containing arrays and strings and other client info, using setObject forKey. Everything looks great when I NSLog the data, format is exactly the way its supposed be. I've also converted my NSDictionary to NSData:
NSData *userData = [NSJSONSerialization dataWithJSONObject:userDict options:NSJSONWritingPrettyPrinted error:&error];
What I need to know is how to upload it to the server using POST. I've found the following code snippet to upload a photo. My question is can I simply use my NSDictionary as a parameter (params in request), it's kinda big. If not, how do I send my NSData object userData? I really love AFNetworking and have been using it exclusively for all my download needs. This is my first time uploading an object.
Thanks
NSData *imageData = UIImagePNGRepresentation(pageImage);
AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:#"http://www.SERVER.com"]];
//You can add POST parameteres here
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
author, #"author",
title, #"title",
nil];
NSMutableURLRequest *request = [client multipartFormRequestWithMethod:#"POST" path:#"/PATH/TO/WEBSERVICE.php" parameters:params constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
//This is the image
[formData appendPartWithFileData: imageData name:#"cover_image" fileName:#"temp.png" mimeType:#"image/png"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
//Setup Upload block to return progress of file upload
[operation setUploadProgressBlock:^(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
float progress = totalBytesWritten / (float)totalBytesExpectedToWrite;
NSLog(#"Upload Percentage: %f %%", progress*100);
}];
//Setup Completeion block to return successful or failure
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *response = [operation responseString];
NSLog(#"response: [%#]",response);
//Code to run after webservice returns success response code
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"error: %#", [operation error]);
//Code to Run if Failed
}];
[operation start];
I am not sure I fully understand you question, but I will give it a try anyway.
My question is can I simply use my NSDictionary as a parameter (params in request), it's kinda big.
I think so. Just give it a try... in the end, those array will be converted in data sent to the server, so if the server is able to handle it, there should be no problem.
If not, how do I send my NSData object userData?
The data you send should be provided where the code you posted specifies imageData. Keep in mind that if what you are uploading is not meant to be a file (you mention NSData), then you might need to use appendPartWithFormData:name. This, again, depends on your server side.
Hope this clarifies things a bit.
I'm trying to to send some post data to a Apache server from iPad application using the ASIHttp library.
actually I need to send huge data to the server and that means I need to compress the request body so I write some code to send the data and compress the request BUT there are no parameters received on the server !!!
the iOS code is :
NSURL * URL = [NSURL URLWithString:myURL];
ASIFormDataRequest *ASIRequest = [ASIFormDataRequest requestWithURL:URL];
ASIRequest.shouldCompressRequestBody=YES;
ASIRequest setPostValue:data forKey:#"data"];
[ASIRequest startSynchronous];
NSError *error = [ASIRequest error];
if (!error) {
NSString *response = [ASIRequest responseString];
NSLog(#"response %#" , response);
}
PS: if I removed the ASIRequest.shouldCompressRequestBody=YES; everything works fine and I can see the data but when use it I see nothing on the server
the request can be seen on the server but with no parameter
noway to send such data over GET method.
the server configuration are fine.
any solution ? any comment or idea can help ?
By default, most web servers do not support compression on POSTs. The accepted answer here does a really good job explainining it: Why can't browser send gzip request?
According to official documentation, this feature has only been tested with Apache servers.
EDIT:
Here is a code snipt that compresses the actual post data:
if ([self shouldCompressRequestBody]) {
NSError *err = nil;
NSData *compressedBody = [ASIDataCompressor compressData:[self postBody] error:&err];
if (err) {
[self failWithError:err];
return;
}
[self setCompressedPostBody:compressedBody];
[self setPostLength:[[self compressedPostBody] length]];
}
Source: http://forums.three20.info/discussion/77/tturlrequest-vs-asihttprequest/p1
I am calling a URL through POST HTTP method. When I am not connected to 3G or WiFi, it will never fetch the URL correctly and will never get the response from the URL correctly.
Connection is made through post:
NSString *URL = [NSString stringWithFormat:#"http://www.abc.com/webservices/client_server.cfc];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
NSString *time_stamp =[NSString stringWithFormat:#"time_stamp=%#", self.today];
NSData *postData = [time_stamp dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d",[postData length]];
NSMutableURLRequest *URLRequest = [[[NSMutableURLRequest alloc] init] autorelease];
[URLRequest setURL:[NSURL URLWithString:URL]];
[exhURLRequest setHTTPMethod:#"POST"];
[URLRequest setValue:postLength forHTTPHeaderField:#"Content-Length"];
[URLRequest setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[URLRequest setHTTPBody:postData];
self.connection =[[[NSURLConnection alloc] initWithRequest:URLRequest delegate:self] autorelease];
NSAssert(self.connection != nil, #"Failure to create URL connection.");
I am checking if I get the response through :
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// check for HTTP status code for proxy authentication failures
// anything in the 200 to 299 range is considered successful
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (([httpResponse statusCode]/100) == 2) {
self.data = [NSMutableData data];
} else {
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:
NSLocalizedString(#"HTTP Error",
#"Error message displayed when receving a connection error.")
forKey:NSLocalizedDescriptionKey];
NSError *error = [NSError errorWithDomain:#"HTTP" code:[httpResponse statusCode] userInfo:userInfo];
[self handleError:error];
}
}
But some how when the URL is not connected lets say when I am in the metro or something, I cannot connect to URL and it throws me "URL NOT SUPPORTED". How can I avoid such a scenario and allow the app to push forward without throwing such an error ? If anybody has used offline redirection to the app without keep on waiting the user on the front screen.
Download appleās Reachability sample code. Add Reachability.h+.m to your project. Link SystemConfiguration framework. Then use their Reachability class like so.
#import "Reachability.h"
for WiFi:
-(void)doStuff{
Reachability *wifi = [Reachability reachabilityForLocalWiFi];
if (wifi.currentReachabilityStatus == ReachableViaWiFi){
// do connected stuff
}
else {
// do offline stuff
}
}
For Wifi or 3G:
-(void)doStuff{
NetworkStatus connectivity = [Reachability reachabilityForInternetConnection].currentReachabilityStatus;
if (connectivity == ReachableViaWiFi || connectivity == ReachableViaWWAN){
// do connected stuff
}
else {
// do offline stuff
}
}
If you will be continually doing network tasks add the reachability object to your class. That will improve performance and allow you to use it's notifier.
some time this "URL NOT SUPPORTED error" is due to the url not encoding.
for this encode url by using this.or there are lots of ways on internet.
dont encode whole url or base url (as it will encode ":" and '/' too and it may cause errors)
only special charecter needs to be encode like &,# etc.
NSString *encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)pathRequest,
NULL,
(CFStringRef)#"!*'();:#&=+$,%#[]",
kCFStringEncodingUTF8 ));
How to check for an active Internet connection on iOS or OSX?
Use the solution propsed here to avoid sending a request when you don't have an active internet connection. Simple.
I use ASIHTTPRequest to do http requests in my iPhone app. ASIHTTPRequet comes with that feature that starts the activity indicator when issuing a request and stops it when finished. The problem is, once I started a request the indicator never stops and keeps spinning as long as my app runs.
Here is my code, a little utility method that fetches some content from the web synchroniously (since it gets started in a different thread):
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL: [NSURL URLWithString: url]];
[request startSynchronous];
NSError *error = [request error];
NSString *response = nil;
if (error) {
NSLog(#"error %#", error);
return nil;
}
int statusCode = [request responseStatusCode];
response = [NSString stringWithString: [request responseString]];
NSLog(#"status code: %d response: %#", statusCode, response);
if (statusCode != 200) {
return nil;
}
return response;
The above code works just fine, I get the contents of the given URL as a NSString only the indicator keeps spinning. My question is: Why does the indicator never stop and how to fix it? Do I have to release some resources here?
This is a bug that was fixed very recently in the development version of ASIHTTPRequest:
http://github.com/pokeb/asi-http-request/commit/35ea592084145b3332861344f36b52dbcaafa351
(It only affects synchronous requests started on a secondary thread)
Can you try the same thing with an asynchronous request and see if that changes it? I use ASIHTTPRequest and I've never noticed this behavior, but I also never use synchronous requests.
I have been working on an iPhone app that has a feature of uploading photos to TwitPic. I have it working with basic authentication.
I am trying to get it working with OAuth. I am getting authentication errors. I have studied very carefully the TwitPic documentation.
I am authorising the app by displaying a UI Web View and the it returns a PIN value. I enter the PIN value in the app and request the token.
I am able to upload status updates to Twitter but not photos.
My code is based on some example code from here:
Example iPhone app using OAuth
Here is my code:
NSString *url = #"http://api.twitpic.com/2/upload.json";
NSString *oauth_header = [oAuth oAuthHeaderForMethod:#"POST" andUrl:url andParams:nil];
NSLog(#"OAuth header : %#\n\n", oauth_header);
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:url]];
[request addRequestHeader:#"User-Agent" value:#"ASIHTTPRequest"];
request.requestMethod = #"POST";
[request addRequestHeader:#"X-Auth-Service-Provider" value:#"https://api.twitter.com/1/account/verify_credentials.json"];
[request addRequestHeader:#"X-Verify-Credentials-Authorization" value:oauth_header];
NSData *imageRepresentation = UIImageJPEGRepresentation(imageToUpload, 0.8);
[request setData:imageRepresentation forKey:#"media"];
[request setPostValue:#"Some Message" forKey:#"message"];
[request setPostValue:TWITPIC_API_KEY forKey:#"key"];
[request setDelegate:self];
[request setDidFinishSelector:#selector(requestDone:)];
[request setDidFailSelector:#selector(requestFailed:)];
[request start];
Here is the OAuth Header:
OAuth realm="http://api.twitter.com/", oauth_timestamp="1275492425", oauth_nonce="b686f20a18ba6763ac52b689b2ac0c421a9e4013", oauth_signature_method="HMAC-SHA1", oauth_consumer_key="zNbW3Xi3MuS7i9cpz6fw", oauth_version="1.0", oauth_token="147275699-jmrjpwk3B6mO2FX2BCc9Ci9CRBbBKYW1bOni2MYs", oauth_signature="d17HImz6VgygZgbcp845CD2qNnI%3D"
HA! I found it!
We should create the header with https://api.twitter.com/1/account/verify_credentials.json and post to http://api.twitpic.com/2/upload.json! (And use GET)
NSString *fakeurl = #"https://api.twitter.com/1/account/verify_credentials.json";
NSString *oauth_header = [oAuth oAuthHeaderForMethod:#"GET" andUrl:fakeurl andParams:nil];
NSLog(#"OAuth header : %#\n\n", oauth_header);
NSString *url = #"http://api.twitpic.com/2/upload.json";
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:url]];
request.delegate = self;
[request addRequestHeader:#"User-Agent" value:#"ASIHTTPRequest"];
request.requestMethod = #"GET";
[request addRequestHeader:#"X-Verify-Credentials-Authorization" value:oauth_header];
[request addRequestHeader:#"X-Auth-Service-Provider" value:#"https://api.twitter.com/1/account/verify_credentials.json"];
NSData *imageRepresentation = UIImageJPEGRepresentation([UIImage imageNamed:#"IMG_0717.jpg"], 0.2);
if (imageRepresentation) {
NSLog(#"Pic not nil");
}
[request setData:imageRepresentation forKey:#"media"];
[request setPostValue:#"twitpic, i hate you. die painfully." forKey:#"message"];
[request setPostValue:twitPicKey forKey:#"key"];
[request setDelegate:self];
[request setDidFinishSelector:#selector(requestDone:)];
[request setDidFailSelector:#selector(requestFailed:)];
[request start];
Use GSTwitPicEngine: https://github.com/Gurpartap/GSTwitPicEngine
Using GSTwitPicEngine:
Initialize the engine with class or as needed:
self.twitpicEngine = (GSTwitPicEngine *)[GSTwitPicEngine twitpicEngineWithDelegate:self];
Find the authorization token and supply to twitpicEngine with:
[twitpicEngine setAccessToken:token];
Then to upload image and attach a text message along with it (does not post to twitter):
[twitpicEngine uploadPicture:[UIImage imageNamed:#"mypic.png"] withMessage:#"Hello world!"]; // This message is supplied back in success delegate call in request's userInfo.
To upload image only:
[twitpicEngine uploadPicture:uploadImageView.image];
Upon end of request, one of the delegate methods is called with appropriate data and information.
GSTwitPicEngineDelegate protocol specifies two delegate methods:
- (void)twitpicDidFinishUpload:(NSDictionary *)response {
NSLog(#"TwitPic finished uploading: %#", response);
// [response objectForKey:#"parsedResponse"] gives an NSDictionary of the response one of the parsing libraries was available.
// Otherwise, use [[response objectForKey:#"request"] objectForKey:#"responseString"] to parse yourself.
if ([[[response objectForKey:#"request"] userInfo] objectForKey:#"message"] > 0 && [[response objectForKey:#"parsedResponse"] count] > 0) {
// Uncomment to update status upon successful upload, using MGTwitterEngine's instance.
// [twitterEngine sendUpdate:[NSString stringWithFormat:#"%# %#", [[[response objectForKey:#"request"] userInfo] objectForKey:#"message"], [[response objectForKey:#"parsedResponse"] objectForKey:#"url"]]];
}
}
and
- (void)twitpicDidFailUpload:(NSDictionary *)error {
NSLog(#"TwitPic failed to upload: %#", error);
if ([[error objectForKey:#"request"] responseStatusCode] == 401) {
// UIAlertViewQuick(#"Authentication failed", [error objectForKey:#"errorDescription"], #"OK");
}
}
All set?
OAuth method to generate a header must be GET. Not POST.
Also url must be https://api.twitter.com/1/account/verify_credentials.json
Thanks, this helped me get it working too :) I also updated http://github.com/jaanus/PlainOAuth with working example code.