Skip to content

Instantly share code, notes, and snippets.

@willhou
Created October 31, 2013 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willhou/7257980 to your computer and use it in GitHub Desktop.
Save willhou/7257980 to your computer and use it in GitHub Desktop.
package com.foursquare.android.sample;
import android.util.Log;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;
public class Puppy {
private static final String TAG = Puppy.class.getSimpleName();
public static String fetch(String url) {
try {
Log.i(TAG, "Fetching: " + url);
URL request = new URL(url);
HttpURLConnection conn = (HttpURLConnection) request.openConnection();
InputStream in = conn.getInputStream();
String json = readStream(in);
Log.i(TAG, "Response: " + json);
return json;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static String post(String url, BasicNameValuePair... nvps) {
try {
Log.i(TAG, "Fetching: " + url);
URL request = new URL(url);
HttpURLConnection conn = (HttpURLConnection) request.openConnection();
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
List<BasicNameValuePair> params = Arrays.asList(nvps);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(params));
writer.close();
os.close();
conn.connect();
InputStream in = conn.getInputStream();
String json = readStream(in);
Log.i(TAG, "Response: " + json);
return json;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private static String getQuery(List<BasicNameValuePair> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (NameValuePair pair : params) {
if (first) {
first = false;
} else {
result.append("&");
}
result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
}
return result.toString();
}
private static String readStream(InputStream in) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
byte[] buffer = new byte[1024];
for (int count; (count = in.read(buffer)) != -1;) {
out.write(buffer, 0, count);
}
return new String(out.toByteArray(), "UTF-8");
} finally {
closeQuietly(out);
}
}
private static void closeQuietly(Closeable closeable) {
try {
if (closeable != null) {
closeable.close();
}
} catch (IOException ioe) {
// no-op
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment