SHARE
TWEET

SEBI

a guest Mar 20th, 2016 20 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Created by Antonio on 26/02/2016.
  3.  */
  4. public class Server {
  5.  
  6.     private static final int CONNECTION_TIMEOUT = 10000;
  7.     private static final int DATARETRIEVAL_TIMEOUT = 10000;
  8.     private static final String SERVER_URL = "http://www.techtalents.info/login_app.php";
  9.     private static String sessionID;
  10.  
  11.     private static String requestUrl(String url, String postParameters) {
  12.  
  13.         HttpURLConnection urlConnection = null;
  14.         try {
  15.             URL urlToRequest = new URL(url);
  16.             urlConnection = (HttpURLConnection) urlToRequest.openConnection();
  17.             urlConnection.setConnectTimeout(CONNECTION_TIMEOUT);
  18.             urlConnection.setReadTimeout(DATARETRIEVAL_TIMEOUT);
  19.  
  20.             if (postParameters != null) {
  21.  
  22.  
  23.                 urlConnection.setDoOutput(true);
  24.                 urlConnection.setRequestMethod("POST");
  25.                 urlConnection.setFixedLengthStreamingMode(
  26.                         postParameters.getBytes().length);
  27.                 urlConnection.setRequestProperty("Content-Type",
  28.                         "application/x-www-form-urlencoded;charset=UTF-8");
  29.  
  30.                 PrintWriter out = new PrintWriter(urlConnection.getOutputStream());
  31.                 out.print(postParameters);
  32.                 out.close();
  33.             }
  34.  
  35.             int statusCode = urlConnection.getResponseCode();
  36.             if (statusCode != HttpURLConnection.HTTP_OK) {
  37.  
  38.             }
  39.  
  40.  
  41.  
  42.             InputStream in =
  43.                     new BufferedInputStream(urlConnection.getInputStream());
  44.             String respuesta = getResponseText(in);
  45.             return respuesta;
  46.  
  47.  
  48.  
  49.  
  50.         } catch (MalformedURLException e) {
  51.  
  52.         } catch (SocketTimeoutException e) {
  53.  
  54.         } catch (IOException e) {
  55.  
  56.         } finally {
  57.             if (urlConnection != null) {
  58.                 urlConnection.disconnect();
  59.             }
  60.         }
  61.  
  62.         return null;
  63.     }
  64.  
  65.     private static String getResponseText(InputStream in) {
  66.         try {
  67.             Reader reader = new InputStreamReader(in, "UTF-8");
  68.             char[]tamanio=new char[6];
  69.             reader.read(tamanio);
  70.             int tam = Integer.parseInt(new String(tamanio));
  71.  
  72.             char[] buffer = new char[tam];
  73.             reader.read(buffer);
  74.             return new String(buffer);
  75.         } catch (UnsupportedEncodingException e) {
  76.             e.printStackTrace();
  77.         } catch (IOException e) {
  78.             e.printStackTrace();
  79.         }
  80.         return "";
  81.     }
  82.  
  83.     public static boolean login(String username, String password) {
  84.         String response = requestUrl(SERVER_URL, "username="+username+"&password="+password);
  85.  
  86.         if(response != null && response.startsWith("OK")){
  87.             sessionID = response.substring(2);
  88.             return true;
  89.         }
  90.         sessionID=null;
  91.         return false;
  92.     }
  93.  
  94.     public static String post(ArrayList<String> parametros){
  95.         StringBuilder sb = new StringBuilder();
  96.         if(sessionID == null){
  97.             return null;
  98.         }
  99.         sb.append("sess_id=" + sessionID);
  100.         for(int i=0;i<parametros.size();i++){
  101.             sb.append("&" + parametros.get(i));
  102.         }
  103.         String response = requestUrl(SERVER_URL, sb.toString());
  104.         Log.i("SERVER:",response);
  105.         return response;
  106.     }
  107. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top