Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.44 KB | None | 0 0
  1. package toServer;
  2.  
  3. import android.os.AsyncTask;
  4.  
  5. import org.json.JSONException;
  6. import org.json.JSONObject;
  7.  
  8. import java.io.*;
  9. import java.net.HttpURLConnection;
  10. import java.net.MalformedURLException;
  11. import java.net.URL;
  12.  
  13.  
  14. public class ConnectToServer extends AsyncTask<String, Void, Void>{
  15.  
  16.     @Override
  17.     protected Void doInBackground(String ... strings){
  18.         String link;
  19.         link = strings[0] + strings[1];
  20.  
  21.         try {
  22.             URL url = new URL(link);
  23.             HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  24.             conn.connect();
  25.  
  26.             InputStream is = conn.getInputStream();
  27.             BufferedReader reader =
  28.                     new BufferedReader(new InputStreamReader(is, "UTF-8"));
  29.  
  30. /*
  31.             String inputLine;
  32.             while ((inputLine = reader.readLine()) != null)
  33.                 System.out.println(inputLine);
  34.             reader.close();
  35. */
  36.             String in,out = null;
  37.             while((in = reader.readLine()) != null)
  38.                       out += in + "\n";
  39.             reader.close();
  40.  
  41.             JSONObject jsonObject = new JSONObject(out);
  42.            
  43.         }
  44.         catch (MalformedURLException e){
  45.             e.printStackTrace();
  46.         }
  47.         catch (IOException e){
  48.             e.printStackTrace();
  49.         }
  50.         catch (JSONException e){
  51.             e.printStackTrace();
  52.         }
  53.         return null;
  54.     }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement