Hi i am beginner in Ios and in my project there is facility for users "ForGotPassword" request
For this i have written some code but that is not working
i.e is objectFromJSONString response shows like -->Message: "No HTTP resource was found that matches the request URI "my url."
what did i do here wrong ,is it my problem or server side problem? please help me
my code:-
- (IBAction)submitBtnClicked:(id)sender {
NSLog(#"password ---->>> %#",self.MedicalIdTxt.text);
NSString *urlStr = [NSString stringWithFormat:#"my url"];
NSLog(#"urlStr --->>> %#",urlStr);
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:urlStr]];
[request setRequestMethod:#"POST"];
[request setPostValue:self.MedicalIdTxt.text forKey:#"MedicaidId"];
[request setDelegate:self];
[request startAsynchronous];
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
}
- (void)requestFinished:(ASIHTTPRequest *)request {
NSLog(#"requestFinished");
[MBProgressHUD hideHUDForView:self.view animated:YES];
NSLog(#"%#",request);
NSString *receivedString = [request responseString];
NSLog(#"receivedString %#",receivedString);
NSDictionary * jsonObj=[[NSDictionary alloc]init];
jsonObj = [receivedString objectFromJSONString];
NSLog(#"jsonObj result is%#",jsonObj);
NSString *messageStr = [jsonObj objectForKey:#"message"];
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:#"" message:messageStr delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alertView show];
}
- (void)requestFailed:(ASIHTTPRequest *)request {
[MBProgressHUD hideHUDForView:self.view animated:YES];
NSString *receivedString = [request responseString];
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:receivedString message:#"" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
here "jsonObj" not showing response which is coming from services what did i do here wrong?
ASIHTTPRequest is not under maintenance since so long, so use AFNetworking instead:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:#"your url"
parameters:#{#"color": #"green"}
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
Related
I am learning AFNetworking kit. I followed this tutorial. Now I stuck at comment 2 see code below AFHTTPRequestOperation.
I imported AFNetworking using cocoapods.
Here is my code:
- (IBAction)jsonTapped:(id)sender{
// comment 1
NSString *string = [NSString stringWithFormat:#"%#weather.php?format=json", BaseURLString];
NSURL *url = [NSURL URLWithString:string];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// comment 2
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// comment 3
self.weather = (NSDictionary *)responseObject;
self.title = #"JSON Retrieved";
[self.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// comment 4
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Error Retrieving Weather"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alertView show];
}];
// comment 5
[operation start];
}
Now getting error at compile time see screen shot:
Please make sure that you included AFNetworking properly inside your project before you import any of the AFNetworking class. Probably you have not imported the kit properly.
Dont use UIAlertView which is deprecated iOS 9 use UIAlertController see code below.
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Title" message:#"text mssg" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:#"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
// Ok action code here
}];
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:#"Other" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
// Other action code here
}];
[alert addAction:okAction];
[alert addAction:otherAction];
[self presentViewController:alert animated:YES completion:nil];
You can use AFHTTPRequestOperationManager
- (void)apiRequestForPOSTWithServiceName:(NSString *)serviceName andParameters:(NSDictionary *)parameter withCompletion:(void(^)(id response, BOOL isSuccess))completion
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
NSString *url = [NSString stringWithFormat:#"%#%#", BASE_URL, serviceName];
[manager POST:url parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
//NSLog(#"JSON: %#", responseObject);
completion(responseObject,YES);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
completion(error,NO);
}];
}
Happy coding...!!
I've to add google autocomplete api in my app but I don't know. I also getting "INVALID REQUEST". I also check developer.google.com but I can't find any solution. I am using AFNetwrok.
Here is my code
NSString *strURL = [NSString stringWithFormat:#"%#key=%#",AutoComplete_URL,GOOGLE_KEY];
NSDictionary *params = #{#"input" : [#"Paris" stringByReplacingOccurrencesOfString:#" " withString:#"+"],#"sensor":#"true"};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:strURL parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (responseObject) {
[APPDELEGATE hideLoadingView];
if ([[responseObject valueForKey:#"status"] isEqualToString:#"OK"]) {
arrAutocomplete = [[responseObject valueForKey:#"predictions"] valueForKey:#"description"];
NSLog(#"AUTO: %#", arrAutocomplete);
}
else{
// [APPDELEGATE showpToastWithTitle:[response valueForKey:#"msg"]];
//[self.tableView setHidden:YES];
}
}
else{
[APPDELEGATE hideLoadingView];
[APPDELEGATE showToastWithTitle:#"Please check your internet connection"];
}
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
[APPDELEGATE hideLoadingView];
[APPDELEGATE showToastWithTitle:#"Something going wrong"];
}];
OUTPUT :-
JSON:
{
predictions = (
);
status = "INVALID_REQUEST";
}
i got it.
NSString *string = [NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/place/autocomplete/json?key=%#&types=geocode&input=%#",GOOGLE_KEY,address];
NSURL *url = [NSURL URLWithString:string];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 2
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
if (responseObject) {
NSLog(#"%#",responseObject);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// 4
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Error Retrieving Weather"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alertView show];
}];
// 5
[operation start];
I am using AFNetworking Framework for calling API and sometimes I am not getting any response from server while API call:
Here is my code snippet:
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"text/html"];
[manager.requestSerializer setTimeoutInterval:timeOutInterval];
[manager POST:[NSString stringWithFormat:#"%#%#",MainUrl,apiName] parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSArray *arrData = responseObject;
NSLog(#"%#",arrData);
NSString *strCode = [NSString stringWithFormat:#"%#",[arrData valueForKey:#"success"]];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[SVProgressHUD dismiss];
NSLog(#"Error: %#", manager);
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Message" message:#"Please check your internet connection" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}];
I get below error :
Error: <AFHTTPRequestOperationManager: 0x80010ce0, baseURL: (null), operationQueue: <NSOperationQueue: 0x80010ca0>{name = 'NSOperationQueue 0x80010ca0'}>
Any help will be appreciated.
In ios 9.1
AFNetworking 2.5
i have login screen for company registration, i want to post all uitextfield data on web service on button clicked .
this is my code for button clicked
- (IBAction)Register:(id)sender {
if (_CompanyName.text.length <=0)
{
NSLog(#"enter company name");
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Enter Company Name"
message:nil
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Continue", nil];
[message setAlertViewStyle:UIAlertViewStyleDefault];
[message show];
}
I am beginners in that type of activity so,if anyone have knowledge then please help,thanks.
Very simple posting data to server coding is below
- (IBAction)actionRegister:(id)sender
{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:strYourURL]];
[request setHTTPMethod:#"POST"];
//Check The Value what we entered in textField
NSLog(#"the company name is:%#",txtFieldCompanyName.text);
NSLog(#"the email is:%#",txtFieldEmail.text);
NSLog(#"the password is:%#",txtFieldPassword.text);
NSLog(#"the password again is:%#",txtFieldPasswordAgain.text);
//Pass The String to server
NSString *strParameters =[NSString stringWithFormat:#"comapny_name=%#&email=%#&password=%#&password_again=%#",txtFieldCompanyName.text,txtFieldEmail.text,txtFieldPassword.text,txtFieldPasswordAgain.text,nil];
//Check The Value what we passed
NSLog(#"the data Details is =%#", strParameters);
//Convert the String to Data
NSData *data1 = [strParameters dataUsingEncoding:NSUTF8StringEncoding];
//Apply the data to the body
[request setHTTPBody:data1];
//Create the response and Error
NSError *err;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];
//This is for Response
NSLog(#"got response==%#", resSrt);
if(resSrt)
{
NSLog(#"got response");
}
else
{
NSLog(#"faield to connect");
}
}
Use AFNetworking class Here is a link
https://github.com/AFNetworking/AFNetworking
http://www.raywenderlich.com/59255/afnetworking-2-0-tutorial
And some peace of code you will get from this tutorial like
- (IBAction)registerButtonTapped:(id)sender
{
// 1
NSString *string = [NSString stringWithFormat:#"www.yourwebapiurl.php?format=json&username=%#", userNameTextField.text];//and so on
NSURL *url = [NSURL URLWithString:string];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 2
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// 3
NSDictionary *responseDict = (NSDictionary *)responseObject;
NSLog(#"You have got the response ");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// 4
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Error Retrieving Weather"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alertView show];
}];
// 5
[operation start];
}
It will Help.Thanks
I want to pass this data using api. I want to get countryname id,code,but I'm not getting how to do.
-(void) getCountries
{
NSString *get=[[NSString alloc]initWithFormat:#"city id=%#,&lang=%#",[self.countryCode text],[self.countryList textInputMode]];
NSLog(#"postDat :%#",get);
NSURL *url=[NSURL URLWithString:#"http://demo28.know3.com/api/country-list/en.html"];
NSData*postData=[get dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request=[[NSMutableURLRequest alloc]init];
[request setURL:url];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"contentLength"];
//[request setValue:#"application/json;charset=UTF-8" forHTTPHeaderField:#"Accept"];
[request setHTTPBody:postData];
NSError *error=[[NSError alloc]init];
NSHTTPURLResponse *response=nil;
NSData *urldata=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(#"Response code %d",(int)[response statusCode]);
if ([response statusCode]<=200 &&[response statusCode]<=300) {
NSString *responseData=[[NSString alloc]initWithData:urldata encoding:NSASCIIStringEncoding];
NSLog(#"Response=%#",responseData);
NSError *error=nil;NSDictionary *jsondata=[NSJSONSerialization JSONObjectWithData:urldata options:NSJSONReadingMutableContainers error:&error];
success=[jsondata[#"success"]integerValue];
NSLog(#"success:%ld",(long)success);
if (success==0) {
NSLog(#"Country");
[self alertStatus:#" country " :#"country List scucess!"];
}else {
NSString *errormsg=(NSString *)jsondata[#"errormesg"];
[self alertStatus:errormsg :#" Failed"];
}
}else{
[self alertStatus:#"connection Failed" :#"sign in failed"];
}
}
-(void)alertStatus:(NSString *)message :(NSString *)title
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:title message:message delegate:self cancelButtonTitle:#"cancel" otherButtonTitles:nil, nil];
[alert show];
}
Have you tried using AFNetworking? It will make your life much more easier.
Download the code from here AFNetworking
And import the AFNetworking folder to your project.
You can use the following code to get country list in an NSDictionary
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:#"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];