Search in sources :

Example 76 with JsonObjectRequest

use of com.android.volley.toolbox.JsonObjectRequest in project aplicativo by InCasa.

the class SensorActivity method getPresenca.

public void getPresenca(String URLPRESENCA) {
    JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, URLPRESENCA, null, new Response.Listener<JSONObject>() {

        // Em caso de sucesso
        @Override
        public void onResponse(JSONObject response) {
            String presenca = "";
            try {
                TextView txtPresenca = (TextView) findViewById(R.id.txtPresenca);
                presenca = response.getString("valor");
                if (presenca.equals("null")) {
                    txtPresenca.setText("N/A");
                } else {
                    if (presenca.equals("1")) {
                        presenca = "Detectado";
                        txtPresenca.setText(presenca);
                    } else {
                        presenca = "Não Detectado";
                        txtPresenca.setText(presenca);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {

        // Em caso de erro
        @Override
        public void onErrorResponse(VolleyError error) {
            Context context = getApplicationContext();
            CharSequence text = "Erro na requisição";
            int duration = Toast.LENGTH_SHORT;
            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
        }
    }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<String, String>();
            // add headers <key,value>
            User user = User.getInstancia();
            String auth = new String(Base64.encode((user.getLogin() + ":" + user.getSenha()).getBytes(), Base64.DEFAULT));
            headers.put("Authorization ", " Basic " + auth);
            return headers;
        }
    };
    // fila de requisições
    RequestQueue fila = Volley.newRequestQueue(this);
    // Adiciona a requisição á fila de requisições
    fila.add(req);
}
Also used : VolleyError(com.android.volley.VolleyError) Context(android.content.Context) User(model.User) HashMap(java.util.HashMap) JSONException(org.json.JSONException) Response(com.android.volley.Response) JSONObject(org.json.JSONObject) Toast(android.widget.Toast) RequestQueue(com.android.volley.RequestQueue) TextView(android.widget.TextView) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest)

Example 77 with JsonObjectRequest

use of com.android.volley.toolbox.JsonObjectRequest in project aplicativo by InCasa.

the class HomeActivity method getSensorUmidade.

public void getSensorUmidade(String URLGETUMIDADE) {
    JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, URLGETUMIDADE, null, new Response.Listener<JSONObject>() {

        // Em caso de sucesso
        @Override
        public void onResponse(JSONObject response) {
            try {
                Umidade umi = Umidade.getInstancia();
                umi.setIdUmidade(response.getInt("id"));
                umi.setNome(response.getString("nome"));
                umi.setDescricao(response.getString("descricao"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {

        // Em caso de erro
        @Override
        public void onErrorResponse(VolleyError error) {
        }
    }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<String, String>();
            // add headers <key,value>
            User user = User.getInstancia();
            String auth = new String(Base64.encode((user.getLogin() + ":" + user.getSenha()).getBytes(), Base64.DEFAULT));
            headers.put("Authorization ", " Basic " + auth);
            return headers;
        }
    };
    // fila de requisições
    RequestQueue fila = Volley.newRequestQueue(this);
    // Adiciona a requisição á fila de requisições
    fila.add(req);
}
Also used : VolleyError(com.android.volley.VolleyError) User(model.User) HashMap(java.util.HashMap) Umidade(model.Umidade) JSONException(org.json.JSONException) Response(com.android.volley.Response) JSONObject(org.json.JSONObject) RequestQueue(com.android.volley.RequestQueue) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest)

Example 78 with JsonObjectRequest

use of com.android.volley.toolbox.JsonObjectRequest in project aplicativo by InCasa.

the class HomeActivity method getCelular.

public void getCelular(String URLGETCELULAR) {
    JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, URLGETCELULAR, null, new Response.Listener<JSONObject>() {

        // Em caso de sucesso
        @Override
        public void onResponse(JSONObject response) {
            try {
                Aplicativo app = Aplicativo.getInstancia();
                app.setIdAplicativo(response.getInt("id"));
                app.setNome(response.getString("nome"));
                app.setMac(response.getString("mac"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {

        // Em caso de erro
        @Override
        public void onErrorResponse(VolleyError error) {
        }
    }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<String, String>();
            // add headers <key,value>
            User user = User.getInstancia();
            String auth = new String(Base64.encode((user.getLogin() + ":" + user.getSenha()).getBytes(), Base64.DEFAULT));
            headers.put("Authorization ", " Basic " + auth);
            return headers;
        }
    };
    // fila de requisições
    RequestQueue fila = Volley.newRequestQueue(this);
    // Adiciona a requisição á fila de requisições
    fila.add(req);
}
Also used : VolleyError(com.android.volley.VolleyError) User(model.User) HashMap(java.util.HashMap) JSONException(org.json.JSONException) Response(com.android.volley.Response) JSONObject(org.json.JSONObject) RequestQueue(com.android.volley.RequestQueue) Aplicativo(model.Aplicativo) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest)

Example 79 with JsonObjectRequest

use of com.android.volley.toolbox.JsonObjectRequest in project aplicativo by InCasa.

the class HomeActivity method getRele2.

public void getRele2(String URLGETRELE2) {
    JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, URLGETRELE2, null, new Response.Listener<JSONObject>() {

        // Em caso de sucesso
        @Override
        public void onResponse(JSONObject response) {
            try {
                Rele2.setIdRele(response.getInt("id"));
                Rele2.setNome(response.getString("nome"));
                Rele2.setDescricao(response.getString("descricao"));
                Rele2.setPorta(response.getInt("porta"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {

        // Em caso de erro
        @Override
        public void onErrorResponse(VolleyError error) {
        }
    }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<String, String>();
            // add headers <key,value>
            User user = User.getInstancia();
            String auth = new String(Base64.encode((user.getLogin() + ":" + user.getSenha()).getBytes(), Base64.DEFAULT));
            headers.put("Authorization ", " Basic " + auth);
            return headers;
        }
    };
    // fila de requisições
    RequestQueue fila = Volley.newRequestQueue(this);
    // Adiciona a requisição á fila de requisições
    fila.add(req);
}
Also used : VolleyError(com.android.volley.VolleyError) User(model.User) HashMap(java.util.HashMap) JSONException(org.json.JSONException) Response(com.android.volley.Response) JSONObject(org.json.JSONObject) RequestQueue(com.android.volley.RequestQueue) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest)

Example 80 with JsonObjectRequest

use of com.android.volley.toolbox.JsonObjectRequest in project aplicativo by InCasa.

the class HomeActivity method getRele3.

public void getRele3(String URLGETRELE3) {
    JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, URLGETRELE3, null, new Response.Listener<JSONObject>() {

        // Em caso de sucesso
        @Override
        public void onResponse(JSONObject response) {
            try {
                Rele3.setIdRele(response.getInt("id"));
                Rele3.setNome(response.getString("nome"));
                Rele3.setDescricao(response.getString("descricao"));
                Rele3.setPorta(response.getInt("porta"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {

        // Em caso de erro
        @Override
        public void onErrorResponse(VolleyError error) {
        }
    }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<String, String>();
            // add headers <key,value>
            User user = User.getInstancia();
            String auth = new String(Base64.encode((user.getLogin() + ":" + user.getSenha()).getBytes(), Base64.DEFAULT));
            headers.put("Authorization ", " Basic " + auth);
            return headers;
        }
    };
    // fila de requisições
    RequestQueue fila = Volley.newRequestQueue(this);
    // Adiciona a requisição á fila de requisições
    fila.add(req);
}
Also used : VolleyError(com.android.volley.VolleyError) User(model.User) HashMap(java.util.HashMap) JSONException(org.json.JSONException) Response(com.android.volley.Response) JSONObject(org.json.JSONObject) RequestQueue(com.android.volley.RequestQueue) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest)

Aggregations

JsonObjectRequest (com.android.volley.toolbox.JsonObjectRequest)134 JSONObject (org.json.JSONObject)129 VolleyError (com.android.volley.VolleyError)106 Response (com.android.volley.Response)103 RequestQueue (com.android.volley.RequestQueue)71 HashMap (java.util.HashMap)69 JSONException (org.json.JSONException)62 User (model.User)50 Gson (com.google.gson.Gson)30 Test (org.junit.Test)22 Context (android.content.Context)19 Toast (android.widget.Toast)17 GsonBuilder (com.google.gson.GsonBuilder)17 NetworkResponse (com.android.volley.NetworkResponse)16 TextView (android.widget.TextView)13 DefaultRetryPolicy (com.android.volley.DefaultRetryPolicy)12 Wallet (it.angelic.mpw.model.jsonpojos.wallet.Wallet)9 ExecutionException (java.util.concurrent.ExecutionException)8 HomeStats (it.angelic.mpw.model.jsonpojos.home.HomeStats)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7