Created
October 31, 2013 22:04
-
-
Save willhou/7257980 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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