Skip to content

Instantly share code, notes, and snippets.

@seaders
Created August 31, 2015 16:35
Show Gist options
  • Save seaders/e6cf8f32afc0a8b288f5 to your computer and use it in GitHub Desktop.
Save seaders/e6cf8f32afc0a8b288f5 to your computer and use it in GitHub Desktop.
package com.sixminute.freeracing;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String url = "https://www.googleapis.com/plus/v1/people/" + Constants.GOOGLE_PLUS_USERID + "?key=";
Button but = (Button)findViewById(R.id.httpbuttuh_and);
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new RequestTask().execute(url + Constants.ANDROID_API_KEY);
}
});
but = (Button)findViewById(R.id.httpbuttuh_ios);
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new RequestTask().execute(url + Constants.IOS_API_KEY);
}
});
but = (Button)findViewById(R.id.httpbuttuh_server);
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new RequestTask().execute(url + Constants.SERVER_API_KEY);
}
});
}
}
class RequestTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... uri) {
Log.d("HTTP GET", uri[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try {
response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
responseString = out.toString();
out.close();
} catch (ClientProtocolException e) {
//TODO Handle problems..
} catch (IOException e) {
//TODO Handle problems..
}
return responseString;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.d("HTTP RESPONSE", result);
}
}
#import "Constants.h"
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction)httpIOS:(id)sender {
[self httpGet:IOS_API_KEY];
}
- (IBAction)httpAndroid:(id)sender {
[self httpGet:ANDROID_API_KEY];
}
- (IBAction)httpServer:(id)sender {
[self httpGet:SERVER_API_KEY];
}
- (void)httpGet:(NSString*)apiKey
{
NSString* url = [NSString stringWithFormat:@"https://www.googleapis.com/plus/v1/people/%@?key=%@",
GOOGLE_PLUS_USERID, apiKey];
NSURLRequest * urlRequest = [ NSURLRequest requestWithURL: [NSURL URLWithString:url] ];
NSURLResponse * response = nil;
NSError * error = nil;
NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response
error:&error];
if (error == nil)
{
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"%@", responseString);
NSLog(@"%@", responseString);
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment