Search in sources :

Example 1 with Temperatura

use of model.Temperatura in project aplicativo by InCasa.

the class HomeActivity method getSensorTemperatura.

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

        // Em caso de sucesso
        @Override
        public void onResponse(JSONObject response) {
            try {
                Temperatura temp = Temperatura.getInstancia();
                temp.setIdTemperatura(response.getInt("id"));
                temp.setNome(response.getString("nome"));
                temp.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) Temperatura(model.Temperatura) 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

RequestQueue (com.android.volley.RequestQueue)1 Response (com.android.volley.Response)1 VolleyError (com.android.volley.VolleyError)1 JsonObjectRequest (com.android.volley.toolbox.JsonObjectRequest)1 HashMap (java.util.HashMap)1 Temperatura (model.Temperatura)1 User (model.User)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1