-3

I'm new to iPhone development,I'm getting access token from Google after that I've decode it,now I just want to check whether He/She is a registered user.I've code in Android,I want to convert it into Objective-C.

class HttpHelper 
{
 private static String token = null;
 public static void setToken(String token) {
 HttpHelper.token = token;
}

public static String getUrlContentAsString(String url, boolean ignoreHeader)
{
 try 
 {
  HttpGet request =new HttpGet(url);
  DefaultHttpClient client = new DefaultHttpClient();
  if (!ignoreHeader && HttpHelper.token != null)
  request.addHeader(new BasicHeader("Authorization",HttpHelper.token));
  HttpResponse response = client.execute(request);
  HttpEntity responseEntity = response.getEntity();

  BufferedReader reader = new BufferedReader(new InputStreamReader(responseEntity.getContent()));
  StringBuilder sb = new StringBuilder();
  while (true) 
  {
   String line = reader.readLine();

  if (line != null)
  sb.append(line);

  else
  break;
  }

 reader.close();

 return sb.toString();
}

catch (Exception e) {
Log.e(

"VerboActivity", e.getMessage());
}

 return null;
}

Can anyone help me? Thanks in advance.

1 Answers1

3

Try this piece of code

 NSMutableURLRequest *request=[[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"GET"];

To set the header use- [request setValue:headerValue forHTTPHeaderField:headerfieldType];

To set the body use- [request setHTTPBody:body(NSData)];

Finally get the response-

NSURLResponse *response;
NSData *responseData=[NSURLConnection sendSynchronousRequest:request     returningResponse:&response error:nil];
Ratikanta Patra
  • 1,177
  • 7
  • 18
  • Thanks a lot....Can you please show me the code for getUrlContentAsString method? that would be better for me. – Nagarajan Karthikeyan May 28 '13 at 10:56
  • You can convert the responseData to string value.Example NSString *str=[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; – Ratikanta Patra May 28 '13 at 10:58
  • Actually Its simple. Ultimately you want the final response to be a string right? My code gives you the final response as binary data i.e NSData in iOS. My last comment shows you how to get String (i.e NSString in iOS) from NSData. Once you do this you have your desired response as string. Just write NSString *str=[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; at the end. The variable "str" is your string. Happy coding:) – Ratikanta Patra May 28 '13 at 11:08
  • Exactly...If you show me the code mean...it would be better – Nagarajan Karthikeyan May 28 '13 at 11:10