Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package <package>;
- import android.util.Log;
- import org.json.JSONObject;
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.net.URLEncoder;
- public class HttpClient {
- public static String sendToken(String token){
- URL url;
- HttpURLConnection connection = null;
- try {
- JSONObject json = new JSONObject();
- json.put("token", token);
- byte[] sent_info = json.toString().getBytes();
- //Create connection
- url = new URL("<url>/register_token");
- connection = (HttpURLConnection)url.openConnection();
- connection.setRequestMethod("POST");
- connection.setRequestProperty("Content-Type", "application/json");
- connection.setRequestProperty("Content-Length", "" +
- Integer.toString(sent_info.length));
- connection.setRequestProperty("Content-Language", "en-US");
- connection.setUseCaches (false);
- connection.setDoInput(true);
- connection.setDoOutput(true);
- // Send POST output.
- DataOutputStream printout = new DataOutputStream(connection.getOutputStream());
- printout.write(sent_info);
- printout.flush();
- printout.close();
- //Get Response
- InputStream is = connection.getInputStream();
- BufferedReader rd = new BufferedReader(new InputStreamReader(is));
- String line;
- StringBuffer response = new StringBuffer();
- while((line = rd.readLine()) != null) {
- response.append(line);
- response.append('\r');
- }
- rd.close();
- return response.toString();
- } catch (Exception e) {
- Log.i(MainActivity.TAG, e.getMessage());
- return null;
- } finally {
- if(connection != null) {
- connection.disconnect();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement