Advertisement
Guest User

Untitled

a guest
May 10th, 2018
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. In ActivtyMain:
  2. Database db = Database.newInstance();
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. db.setURL("http://192.168.30.1:28389/");
  8. try {
  9.  
  10. getAllViews();
  11. } catch (Exception e) {
  12. Toast.makeText(this,"exception"+e.getMessage(),Toast.LENGTH_LONG).show();
  13. }
  14.  
  15. }
  16.  
  17. My Database class:
  18.  
  19. package com.example.schueler.project.data;
  20.  
  21. import com.example.schueler.project.misc.ControllerSync;
  22. import com.google.gson.Gson;
  23. import com.google.gson.reflect.TypeToken;
  24.  
  25. import java.lang.reflect.Type;
  26. import java.util.ArrayList;
  27.  
  28. public class Database {
  29. private static Database db=null;
  30. private static ControllerSync controller = null;
  31. private static String url = null;
  32.  
  33. private Database() {
  34. }
  35.  
  36. public static Database newInstance(){
  37. if(db==null)
  38. db= new Database();
  39. return db;
  40. }
  41.  
  42. public static void setURL(String _url){
  43. url=_url;
  44. }
  45.  
  46. public ArrayList<User> getUsers() throws Exception{
  47. Gson gson = new Gson();
  48. //each call needs an new instance of async !!
  49. controller = new ControllerSync(url);
  50.  
  51. controller.execute("users");
  52. String strFromWebService = controller.get();
  53. System.out.println("=========" + strFromWebService);
  54. Type collectionType = new TypeToken<ArrayList<User>>() {}.getType();
  55. System.out.println("ct:"+collectionType);
  56. ArrayList<User> vec = gson.fromJson(strFromWebService, collectionType);
  57.  
  58. return vec;
  59. }
  60.  
  61.  
  62.  
  63. }
  64.  
  65. And finally the controller class:
  66.  
  67. package com.example.schueler.project.misc;
  68.  
  69. import android.os.AsyncTask;
  70. import java.io.BufferedReader;
  71. import java.io.InputStreamReader;
  72. import java.net.URL;
  73. import java.net.URLConnection;
  74.  
  75. import android.os.AsyncTask;
  76. import java.io.BufferedOutputStream;
  77. import java.io.BufferedWriter;
  78. import java.io.OutputStreamWriter;
  79. import java.net.HttpURLConnection;
  80.  
  81. public class ControllerSync extends AsyncTask<String, Void, String> {
  82.  
  83. private static final String URI_2ND = "BummWebService/webresources/";
  84. private String url_1st;
  85.  
  86. public ControllerSync(String url) throws Exception {
  87. this.url_1st = url;
  88. }
  89.  
  90. @Override
  91. protected String doInBackground(String... command) {
  92. boolean isGet = true, isPut = false;
  93.  
  94. BufferedReader reader = null;
  95. BufferedWriter writer = null;
  96. String content = null;
  97. URL url = null;
  98.  
  99. try {
  100. if (command[0].equals("users")) {
  101. //call producerlist
  102. url = new URL(url_1st + URI_2ND + "users");
  103. }
  104.  
  105. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  106. if (isPut) {
  107. conn.setRequestMethod("PUT");
  108. conn.setRequestProperty("Content-Type", "application/json");
  109. writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()));
  110. writer.write(command[1]); //product - object in json-format
  111. writer.flush();
  112. writer.close();
  113. conn.getResponseCode();
  114. }
  115. // get data from server
  116. reader = new BufferedReader(new InputStreamReader(
  117. conn.getInputStream()));
  118. StringBuilder sb = new StringBuilder();
  119. String line = null;
  120.  
  121. while ((line = reader.readLine()) != null) {
  122. sb.append(line);
  123. }
  124.  
  125. content = sb.toString();
  126. reader.close();
  127. conn.disconnect();
  128. } catch (Exception ex) {
  129. content = "error in doInBackground: " + ex.getMessage();
  130. }
  131. return content;
  132.  
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement