json data reply nil - iphone

NSString *urlString =[NSString stringWithFormat:#"http://api.stackoverflow.com/1.0/questions?tagged=iphone & key=%d",key1];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:#"POST"];
[request setValue:#"text/json" forHTTPHeaderField:#"Content-Type"];
[request setValue:#"text/json" forHTTPHeaderField:#"Accept"];
NSError *error ;
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSLog(#"jsondata=%#,%#",json_string,response);
2011-02-11 12:01:16.653
mystack[1946:207] jsondata=,(null)

Your URL string is not valid since it contains blank spaces: &. You need to escape those before creating an NSURL object. Replace:
NSString *urlString =[NSString stringWithFormat:#"http://api.stackoverflow.com/1.0/questions?tagged=iphone & key=%d",key1];
with:
NSString *urlString =[NSString stringWithFormat:#"http://api.stackoverflow.com/1.0/questions?tagged=iphone & key=%d",key1];
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

thanks for helping me here is the answer
[request setHTTPMethod:#"POST"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
the code that i have to change..

You should NSLog your error variable to see what happened: [error localizedDescription]

Related

Get a request twice?

I am using the below code for send a request to web-service and get the response , but here i got the request twice at a time ,i dont where i did mistake,help me to come out from this issue. thanks in advance.
NSString *poststr=[NSString stringWithFormat:#"&cname=%#&conname=%#&email=%#",companynametxt.text,contactnametxt.text,contactEmailtxt.text];
NSLog(#"poststr %#",poststr);
NSData *postData = [poststr dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString: #"web servicess"]];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:postData];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
NSLog(#"DATA%#",theConnection);
[theConnection release];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *filenameStr=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(#"filenameStr %#",filenameStr);
You are making two separate calls
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
and
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
use either one of them not both.

Posting data from iphone app to server

I am sending data from iphone application to server but when it reads post String it crashes in the application and do not move forward
-(void)submitSurveyAnswers{
NSString*survey_question_response_id="1";
NSString*survey_id=#"1";
NSString *question_id =#"1";
NSString *survey_response_answer_id =#"1";
NSString *post =[[NSString alloc] initWithFormat:#"survey_question_response_id=%#&survey_id=%#&question_id=%#&survey_response_answer_id=%#",survey_question_response_id,survey_id,question_id,survey_response_answer_id];
NSLog(post);
NSURL *url=[NSURL URLWithString:#"http://myserver-solutions.com/app/surveyAnswer.php?"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:url];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(#"%#",data);
}
You have created one string with below way:
NSString*survey_question_response_id="1"; //Missing '#' while initializing string
It should be created like below way:
NSString*survey_question_response_id=#"1"; //Added '#' while initializing string
NSString*survey_question_response_id="1";
is wrong. Strings between plain double quotes are C string (0-terminated character pointers), and aren't valid NSString instances. I'm sure you wanted to 1. learn to use a debugger 2. write
NSString *survey_question_response_id = #"1";
instead.

i want to send request by POST on iPhone,but the body can't be received by server [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
iPhone SDK Xcode 4.2 iOS 5- How do i send a json to url ? (POST not GET) [Resolved]
NSDictionary *requestDic=[NSDictionary dictionaryWithObjects:values forKeys:keys];
NSString *jsonreq=[requestDic JSONRepresentation];
NSURL *requrl=[NSURL URLWithString:[NSString stringWithFormat:#"%#index.php?r=%#&te=%#&noCheck=1&checkInstall=start",[Info serverUrl],[dictParameters objectForKey:#"r"],[dictParameters objectForKey:#"te"]]];
requrl = [NSURL URLWithString:[[requrl absoluteString] stringByAppendingString:subUrl]];
NSMutableURLRequest *req=[NSMutableURLRequest requestWithURL:requrl];
[req setHTTPMethod:#"POST"];
[req setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[req setValue:#"application/json-x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
NSData *requestData=[NSData dataWithBytes:[jsonreq UTF8String] length:[jsonreq length]];
[req setHTTPBody:requestData];
[browser loadRequest:req]
Dilshand's answer is ok , but you can use ASIFormDataRequest for ease. like follow
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:#"POST"];
[request setPostValue:mobileApplicationId forKey:#"mobileApplicationId"];
[request setPostValue:mobileApplicationVersion forKey:#"mobileApplicationVersion"];
[request setPostValue:userID forKey:#"userId"];
[request setPostValue:pass forKey:#"password"];
[request setValidatesSecureCertificate:NO];
[request setDelegate:self];
[request setTimeOutSeconds:60];
[request startAsynchronous];
What you want to post server give complete detail and what error you are getting
try following i have done using this to send data to server and it works fine for me hope this helps
NSString *post =[[NSString alloc] initWithFormat:#"ProviderNPI=%#&PatientID=%#&FileURL=%#&FileTYPE=%#&DataSynID=%#& AppointmentListingsID=%#",providerNPI,patientID,FILEURL,FILETYPE,dataSynID,appointmentListingID];
NSURL *url=[NSURL URLWithString:#"http://myserver.com/Sync.php?"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:url];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(#"%#",data);
NSURL *url = [NSURL URLWithString:#"http://www.invoicera.com/app/api/check_json_api.php?token=7B92C122473A3D6F54E60D20AC5526D0"];
NSString *jsonRequest = [NSString stringWithFormat:#"&json_data=%#",[[NSString stringWithFormat:#"{\"listInvoice\":{\"client_id\":\"\",\"date_from\":\"\",\"date_to\":\"\",\"invoice_number\":\"\",\"invoice_record_status\":\"\",\"invoice_status\":\"\",\"page\":\"1\",\"per_page_record\":\"20\"}}"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog(#"%#",jsonRequest);
NSData *json_data = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
NSLog(#"%#",json_data);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:#"POST"];
[request setHTTPBody: json_data];
NSLog(#"%#",json_data);
// [request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
//[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setValue:[NSString stringWithFormat:#"%d", [json_data length]] forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:[[jsonRequest stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]
dataUsingEncoding:NSUTF8StringEncoding
allowLossyConversion:YES]];
// [NSURLConnection connectionWithRequest:[request autorelease] delegate:self];
NSURLConnection *nsUrlConnection=[[NSURLConnection alloc]
initWithRequest:request
delegate:self];
// Successful connection.
if (nsUrlConnection) {
// [self initSpinner];
// [self spinBegin];
NSMutableData *data = [[NSMutableData alloc] init];
self.receivedData=data;
[data release];
}
// Unsuccessful connection.
else {
}
// Clean up
[url release];
[request release];
// Close keypad.

How to use .NET web service in iPhone, with POST method?

I am communicating with a .NET web service in an iPhone application, using the POST method, but I am not getting XML back as response. Here is my code:
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"Myserver/EventService.svc"]];
NSString *api = #"/registeruser";
NSData *body = [[NSString stringWithFormat:#"%#/%#/%#/%#/%#/%#/%#/%#/%#",
api, txtFName.text ,txtLName.text,
txtUName.text, txtEmail.text, txtPassword.text,
txtCity.text, txtState.text, str] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSLog(#"url %#",url);
NSLog(#"body %#",[NSString stringWithFormat:#"%#/%#/%#/%#/%#/%#/%#/%#/%#",
api, txtFName.text, txtLName.text,
txtUName.text, txtEmail.text, txtPassword.text,
txtCity.text, txtState.text, str]);
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request setHTTPBody:body];
[request setValue:#"text/xml" forHTTPHeaderField:#"Content-Type"];
NSLog(#"body len %d",[body length]);
NSString *postLength = [NSString stringWithFormat:#"%d", [body length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPMethod:#"POST"];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *XMLString=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(#"data %#",XMLString);
Please help me. Thanks in advance.
I have done that with following.
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:#"Myserver/EventService.svc/webservice/%#/%#",para1,para2]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request addValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request addValue:0 forHTTPHeaderField:#"Content-Length"];
[request setHTTPMethod:#"POST"];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *XMLString=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(#"data %#",XMLString);
Have you tried something smaller first? Say doing an http post locally? Or doing an http post to that server with a simple html form?
I have run into this issue myself and decided to step back and start with a simple http post to make sure my objective-c was correct first.
I used this tutorial to get me started -hope it helps

how to pass arguments to url (.php file)

http://something.php?ownerID=13&view=userAgreement
i need to pass two variables to this ..
how can i pass these i am declared ownerid to 13 in my config playlist and i am able to get it ,how to set "view = useragreement" string to view variable....
can any body show me sample code..
i am using like
NSString* termsURL = [[[NSBundle mainBundle] infoDictionary] objectForKey:#"termsURL"];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"%#%#%#%#%#", termsURL, #"?ownerID=", ownerID,#"?view=",userAgreement]];
but is not working,i am checking parsing is success or not with this url but is failing...
Code taken straight from my app. It works for what you need
NSString *post = [NSString stringWithFormat:#"ownerID=13&view=userAgreement"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
//set up the request to the website
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:#"http://something.php"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *result3=[[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]autorelease];