Created
December 23, 2011 16:40
-
-
Save ziglee/1514695 to your computer and use it in GitHub Desktop.
Android Rest Json Client
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
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import org.apache.http.HttpEntity; | |
import org.apache.http.HttpResponse; | |
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 org.apache.http.params.HttpConnectionParams; | |
import org.apache.http.params.HttpParams; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
public class RestJsonClient { | |
public static JSONObject connect(String url) throws JSONException, ClientProtocolException, IOException { | |
HttpClient httpclient = new DefaultHttpClient(); | |
HttpParams params = httpclient.getParams(); | |
HttpConnectionParams.setConnectionTimeout(params, 0); | |
HttpConnectionParams.setSoTimeout(params, 0); | |
HttpGet httpget = new HttpGet(url); | |
JSONObject json = new JSONObject(); | |
HttpResponse response = httpclient.execute(httpget); | |
HttpEntity entity = response.getEntity(); | |
if (entity != null) { | |
InputStream instream = entity.getContent(); | |
String result = convertStreamToString(instream); | |
json = new JSONObject(result); | |
instream.close(); | |
} | |
return json; | |
} | |
public static String convertStreamToString(InputStream is) throws IOException { | |
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); | |
StringBuilder sb = new StringBuilder(); | |
String line = null; | |
try { | |
while ((line = reader.readLine()) != null) { | |
sb.append(line + "\n"); | |
} | |
} catch (IOException e) { | |
throw e; | |
} finally { | |
try { | |
is.close(); | |
} catch (IOException e) { | |
throw e; | |
} | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment