Created
December 2, 2014 20:57
-
-
Save devrath/400af10db619da1e4861 to your computer and use it in GitHub Desktop.
GET AND POST REQUEST
This file contains hidden or 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
| public class UrlConnection { | |
| private static final String TAG = "DownloadData"; | |
| public static String executeGet(String requestUrl) | |
| throws IOException { | |
| InputStream inputStream = null; | |
| String response=""; | |
| HttpURLConnection urlConnection = null; | |
| /* forming th java.net.URL object */ | |
| URL url = new URL(requestUrl); | |
| urlConnection = (HttpURLConnection) url.openConnection(); | |
| /* optional request header */ | |
| urlConnection.setRequestProperty("Content-Type", "application/json"); | |
| /* optional request header */ | |
| urlConnection.setRequestProperty("Accept", "application/json"); | |
| /* for Get request */ | |
| urlConnection.setReadTimeout(10000 /* milliseconds */); | |
| urlConnection.setConnectTimeout(15000 /* milliseconds */); | |
| urlConnection.setRequestMethod("GET"); | |
| urlConnection.setDoInput(true); | |
| // Starts the query | |
| urlConnection.connect(); | |
| int statusCode = urlConnection.getResponseCode(); | |
| Log.d(TAG, "The response is: " + statusCode); | |
| /* 200 represents HTTP OK */ | |
| if (statusCode == 200) { | |
| inputStream = new BufferedInputStream( | |
| urlConnection.getInputStream()); | |
| response = convertInputStreamToString(inputStream); | |
| // String[] results = parseResult(response); | |
| } | |
| return response; | |
| } | |
| public static String executePost(String requestUrl, String urlParameters) | |
| throws IOException{ | |
| InputStream inputStream = null; | |
| String response=""; | |
| HttpURLConnection urlConnection = null; | |
| /* forming th java.net.URL object */ | |
| URL url = new URL(requestUrl); | |
| urlConnection = (HttpURLConnection) url.openConnection(); | |
| urlConnection.setReadTimeout(10000 /* milliseconds */); | |
| urlConnection.setConnectTimeout(15000 /* milliseconds */); | |
| urlConnection.setRequestMethod("POST"); | |
| urlConnection.setDoInput(true); | |
| /* optional request header */ | |
| urlConnection.setRequestProperty("Content-Type", "application/json"); | |
| /* optional request header */ | |
| urlConnection.setRequestProperty("Accept", "application/json"); | |
| DataOutputStream wr = new DataOutputStream( | |
| urlConnection.getOutputStream()); | |
| wr.writeBytes(urlParameters); | |
| wr.flush(); | |
| wr.close(); | |
| /* for Get request */ | |
| // Starts the query | |
| urlConnection.connect(); | |
| int statusCode = urlConnection.getResponseCode(); | |
| Log.d(TAG, "The response is: " + statusCode); | |
| /* 200 represents HTTP OK */ | |
| if (statusCode == 200) { | |
| inputStream = new BufferedInputStream( | |
| urlConnection.getInputStream()); | |
| response = convertInputStreamToString(inputStream); | |
| // String[] results = parseResult(response); | |
| } | |
| return response; | |
| } | |
| private static String convertInputStreamToString(InputStream inputStream) { | |
| StringBuilder sb = new StringBuilder(); | |
| try { | |
| Reader r = new InputStreamReader(inputStream); | |
| char[] chars = new char[1024]; | |
| int len; | |
| while ((len = r.read(chars)) >= 0) { | |
| sb.append(chars, 0, len); | |
| } | |
| } catch (Exception ex) { | |
| Log.d(TAG, | |
| "Error converting inputstream to string " | |
| + ex.getLocalizedMessage()); | |
| } finally { | |
| CloseStream.closeInputStream(inputStream); | |
| } | |
| return sb.toString(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment