Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- In ActivtyMain:
- Database db = Database.newInstance();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- db.setURL("http://192.168.30.1:28389/");
- try {
- getAllViews();
- } catch (Exception e) {
- Toast.makeText(this,"exception"+e.getMessage(),Toast.LENGTH_LONG).show();
- }
- }
- My Database class:
- package com.example.schueler.project.data;
- import com.example.schueler.project.misc.ControllerSync;
- import com.google.gson.Gson;
- import com.google.gson.reflect.TypeToken;
- import java.lang.reflect.Type;
- import java.util.ArrayList;
- public class Database {
- private static Database db=null;
- private static ControllerSync controller = null;
- private static String url = null;
- private Database() {
- }
- public static Database newInstance(){
- if(db==null)
- db= new Database();
- return db;
- }
- public static void setURL(String _url){
- url=_url;
- }
- public ArrayList<User> getUsers() throws Exception{
- Gson gson = new Gson();
- //each call needs an new instance of async !!
- controller = new ControllerSync(url);
- controller.execute("users");
- String strFromWebService = controller.get();
- System.out.println("=========" + strFromWebService);
- Type collectionType = new TypeToken<ArrayList<User>>() {}.getType();
- System.out.println("ct:"+collectionType);
- ArrayList<User> vec = gson.fromJson(strFromWebService, collectionType);
- return vec;
- }
- }
- And finally the controller class:
- package com.example.schueler.project.misc;
- import android.os.AsyncTask;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.net.URL;
- import java.net.URLConnection;
- import android.os.AsyncTask;
- import java.io.BufferedOutputStream;
- import java.io.BufferedWriter;
- import java.io.OutputStreamWriter;
- import java.net.HttpURLConnection;
- public class ControllerSync extends AsyncTask<String, Void, String> {
- private static final String URI_2ND = "BummWebService/webresources/";
- private String url_1st;
- public ControllerSync(String url) throws Exception {
- this.url_1st = url;
- }
- @Override
- protected String doInBackground(String... command) {
- boolean isGet = true, isPut = false;
- BufferedReader reader = null;
- BufferedWriter writer = null;
- String content = null;
- URL url = null;
- try {
- if (command[0].equals("users")) {
- //call producerlist
- url = new URL(url_1st + URI_2ND + "users");
- }
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- if (isPut) {
- conn.setRequestMethod("PUT");
- conn.setRequestProperty("Content-Type", "application/json");
- writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()));
- writer.write(command[1]); //product - object in json-format
- writer.flush();
- writer.close();
- conn.getResponseCode();
- }
- // get data from server
- reader = new BufferedReader(new InputStreamReader(
- conn.getInputStream()));
- StringBuilder sb = new StringBuilder();
- String line = null;
- while ((line = reader.readLine()) != null) {
- sb.append(line);
- }
- content = sb.toString();
- reader.close();
- conn.disconnect();
- } catch (Exception ex) {
- content = "error in doInBackground: " + ex.getMessage();
- }
- return content;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement