Last active
November 18, 2015 18:47
-
-
Save carloscarucce/3828b4dcc8a58f2089d0 to your computer and use it in GitHub Desktop.
Android - webserver request with JSON response
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.xxnoobmanxx.util; | |
import android.util.Pair; | |
import org.json.JSONObject; | |
import java.io.ByteArrayOutputStream; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.io.UnsupportedEncodingException; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by carucce on 17/11/2015. | |
*/ | |
public final class WebserverRequest { | |
public final static String CHARSET = "UTF8"; | |
/** | |
* | |
* @param address | |
* @return | |
*/ | |
public static JSONObject send(String address){ | |
List<Pair<String,String>> params = new ArrayList<Pair<String, String>>(); | |
return send(address, params); | |
} | |
/** | |
* | |
* @param address | |
* @param params | |
* @return | |
*/ | |
public static JSONObject send(String address, List<Pair<String,String>> params){ | |
JSONObject response = null; | |
try { | |
URL url = new URL(address); | |
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |
connection.setConnectTimeout(3000); | |
connection.setReadTimeout(3000); | |
connection.setDoOutput(true); | |
connection.setRequestMethod("POST"); | |
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); | |
connection.connect(); | |
OutputStream outputStream = connection.getOutputStream(); | |
outputStream.write(getQueryBytes(params)); | |
outputStream.flush(); | |
outputStream.close(); | |
if(connection.getResponseCode() == HttpURLConnection.HTTP_OK){ | |
InputStream inputStream = connection.getInputStream(); | |
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream (); | |
byte[] buffer = new byte[1024]; | |
int len; | |
while ((len = inputStream.read(buffer)) > 0){ | |
byteArrayOutputStream.write(buffer, 0, len); | |
} | |
byte[] responseBytes = byteArrayOutputStream.toByteArray(); | |
byteArrayOutputStream.close(); | |
inputStream.close(); | |
String responseString = new String(responseBytes); | |
response = new JSONObject(responseString); | |
} | |
}catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return response; | |
} | |
/** | |
* | |
* @param params | |
* @return | |
* @throws UnsupportedEncodingException | |
*/ | |
private static byte[] getQueryBytes(List<Pair<String,String>> params) throws UnsupportedEncodingException { | |
StringBuffer buffer = new StringBuffer(); | |
for(Pair<String,String> pair : params){ | |
String first = URLEncoder.encode(pair.first, CHARSET); | |
String second = URLEncoder.encode(pair.second, CHARSET); | |
buffer.append(first + "=" + second + "&"); | |
} | |
String finalString = buffer.toString(); | |
finalString = (finalString.length() > 0 ? finalString.substring(0, finalString.length() - 1) : ""); | |
return finalString.getBytes(CHARSET); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment