Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.26 KB | None | 0 0
  1. package com.example.administrator.a0502;
  2.  
  3. import android.content.Context;
  4. import android.os.AsyncTask;
  5.  
  6. import java.io.BufferedReader;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. import java.net.HttpURLConnection;
  10. import java.net.MalformedURLException;
  11. import java.net.URL;
  12.  
  13. public class LoginClass extends AsyncTask<Object, Object, String> {
  14.  
  15.     private final Context context;
  16.  
  17.  
  18.     public LoginClass(Context c){
  19.         this.context = c;
  20.     }
  21.  
  22.     @Override
  23.     protected String doInBackground(Object[] objects) {
  24.         return posaljiZahtev();
  25.     }
  26.  
  27.  
  28.     protected void onPreExecute(){
  29.     }
  30.  
  31.     @Override
  32.     protected void onPostExecute(String result) {
  33.     }
  34.  
  35.  
  36.     public String posaljiZahtev()
  37.     {
  38.  
  39.         String lokacijaServera = this.context.getString(R.string.lokacijaServera);
  40.  
  41.  
  42.         try {
  43.             URL url = new URL(lokacijaServera);
  44.  
  45.             HttpURLConnection connection = (HttpURLConnection)url.openConnection();
  46.  
  47.  
  48.             connection.setRequestMethod("POST");
  49.             connection.setRequestProperty("USER-AGENT", "Mozilla/5.0");
  50.             connection.setRequestProperty("ACCEPT-LANGUAGE", "en-US,en;0.5");
  51.             connection.setDoOutput(true);
  52.  
  53.  
  54.  
  55.             StringBuilder output = new StringBuilder();
  56.             BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  57.             String line = "";
  58.             StringBuilder responseOutput = new StringBuilder();
  59.             //System.out.println("output===============" + br);
  60.             while((line = br.readLine()) != null ) {
  61.                 responseOutput.append(line);
  62.             }
  63.  
  64.             br.close();
  65.             //output.append(System.getProperty("line.separator") + "Response " + System.getProperty("line.separator") + System.getProperty("line.separator") + responseOutput.toString());
  66.             output.append(responseOutput.toString());
  67.  
  68.  
  69.             System.out.println("60.... " + output.toString());
  70.  
  71.             //return null;
  72.  
  73.             return output.toString();
  74.  
  75.         } catch (MalformedURLException e) {
  76.             e.printStackTrace();
  77.         } catch (IOException e) {
  78.             e.printStackTrace();
  79.         }
  80.  
  81.         return null;
  82.  
  83.     }
  84.  
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement