Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package th.ac.ictsu.helloadapternoon;
- import android.os.StrictMode;
- import android.util.Log;
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.HashMap;
- public class ServerConnecter {
- public static String http = "http://";
- public static String ip = "172.16.30.13/";
- public static String dir = "016/";
- public ServerConnecter()
- {
- StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
- StrictMode.setThreadPolicy(policy);
- }
- public String connect (String page,boolean post,boolean response,EntityModel model)
- {
- int responsecode = 0;
- StringBuilder result = new StringBuilder();
- String url = http+ip+dir+page;
- HttpURLConnection connection = null;
- try {
- URL urlcon = new URL(url);
- connection = (HttpURLConnection) urlcon.openConnection();
- connection.setDoInput(true);
- if (post == true)
- {
- connection.setDoOutput(true);
- connection.setRequestMethod("POST");
- connection.setConnectTimeout(5000);
- connection.setRequestProperty("Charset", "UTF-8");
- DataOutputStream outstream = new DataOutputStream(connection.getOutputStream());
- outstream.write(model.getDataEncode());
- }
- if (response == true)
- {
- String line;
- BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
- while ((line = br.readLine()) != null)
- {
- result.append(line);
- }
- }
- responsecode = connection.getResponseCode();
- Log.i("HTTP_connect", "Response Code : " + responsecode);
- } catch (MalformedURLException e) {
- Log.i("HTTP_connect","URL Error");
- e.printStackTrace();
- } catch (IOException e) {
- Log.i("HTTP_connect","Connection Error");
- e.printStackTrace();
- }
- connection.disconnect();
- return result.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement