Back to project page UTHPortal-Android-Gradle.
The source code is released under:
MIT License
If you think the Android project UTHPortal-Android-Gradle listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.uth.uthportal.network; // w w w .ja v a 2s . co m import android.util.Log; import java.io.ByteArrayOutputStream; import java.io.IOException; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; 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; /** * @author GeorgeT * */ public class JSONDownloader{ public static String getJSON(String URI){ HttpClient httpClient = new DefaultHttpClient(); //http client for the request HttpResponse wResponse; //httpResponse String responseStr = null; //the actual response string try { wResponse = httpClient.execute(new HttpGet(URI)); StatusLine sLine = wResponse.getStatusLine(); //get response status if(sLine.getStatusCode()== HttpStatus.SC_OK){ //request was ok. ByteArrayOutputStream out = new ByteArrayOutputStream(); //create output stream wResponse.getEntity().writeTo(out); //write our response to the stream out.close(); responseStr = out.toString(); } else { Log.e("NetError (" + URI + ")", sLine.getReasonPhrase()); //something went wrong wResponse.getEntity().getContent().close();//close the connection return null; //throw the error status } } catch (ClientProtocolException e) { return null; //TODO Handle this } catch (IOException e) { return null; //TODO Handle this } return responseStr; } }