Search in sources :

Example 36 with Response

use of com.android.volley.Response in project aplicativo by InCasa.

the class LoginActivity method Login.

public void Login(String URLLOGIN) {
    JsonObjectRequest req = new JsonObjectRequest(Request.Method.POST, URLLOGIN, null, new Response.Listener<JSONObject>() {

        // Em caso de sucesso
        @Override
        public void onResponse(JSONObject response) {
            boolean Authorization = false;
            try {
                Authorization = response.getBoolean("Authorized");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            if (Authorization == true) {
                // Obtem a instancia vazia do User
                User user = User.getInstancia();
                // Adiciona login e senha na instancia
                user.setLogin(login);
                user.setSenha(senha);
                Intent it = new Intent(LoginActivity.this, HomeActivity.class);
                startActivity(it);
                finish();
            }
        }
    }, new Response.ErrorListener() {

        // Em caso de erro
        @Override
        public void onErrorResponse(VolleyError error) {
            Context context = getApplicationContext();
            CharSequence text = "Login ou Senha incorretos";
            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>
            String auth = new String(Base64.encode((login + ":" + senha).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) Intent(android.content.Intent) Response(com.android.volley.Response) JSONObject(org.json.JSONObject) Toast(android.widget.Toast) RequestQueue(com.android.volley.RequestQueue) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest)

Example 37 with Response

use of com.android.volley.Response in project aplicativo by InCasa.

the class ReleActivity method getRele2.

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

        // Em caso de sucesso
        @Override
        public void onResponse(JSONObject response) {
            String temp = "";
            try {
                Switch rele2 = (Switch) findViewById(R.id.switch2);
                temp = response.getString("valor");
                if (temp.equals("0")) {
                    rele2.setChecked(false);
                    estado2 = false;
                } else {
                    rele2.setChecked(true);
                    estado2 = true;
                }
            } 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) Switch(android.widget.Switch) Toast(android.widget.Toast) RequestQueue(com.android.volley.RequestQueue) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest)

Example 38 with Response

use of com.android.volley.Response in project aplicativo by InCasa.

the class SensorActivity method getUmidade.

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

        // Em caso de sucesso
        @Override
        public void onResponse(JSONObject response) {
            String umi = "";
            try {
                TextView txtUmi = (TextView) findViewById(R.id.txtUmi);
                umi = response.getString("valor");
                if (umi.equals("null")) {
                    txtUmi.setText("N/A");
                } else {
                    umi = umi + "%";
                    txtUmi.setText(umi);
                }
            } 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 39 with Response

use of com.android.volley.Response in project aplicativo by InCasa.

the class SensorActivity method getLuminosidade.

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

        // Em caso de sucesso
        @Override
        public void onResponse(JSONObject response) {
            String lumi = "";
            try {
                TextView txtLumi = (TextView) findViewById(R.id.txtLumi);
                lumi = response.getString("valor");
                if (lumi.equals("null")) {
                    txtLumi.setText("N/A");
                } else {
                    txtLumi.setText(lumi);
                }
            } 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 40 with Response

use of com.android.volley.Response in project aplicativo by InCasa.

the class VozActivity method enviaComando.

public void enviaComando(JSONObject json, String URLCOMANDO) {
    JsonObjectRequest req = new JsonObjectRequest(Request.Method.POST, URLCOMANDO, json, new Response.Listener<JSONObject>() {

        // Em caso de sucesso
        @Override
        public void onResponse(JSONObject response) {
            Context context = getApplicationContext();
            CharSequence text = "Comando enviado";
            int duration = Toast.LENGTH_SHORT;
            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
            try {
                String resposta = response.getString("valor");
                txtSpeechOutput.setText(resposta);
                txtSpeechInput.setText("");
            } 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 : Context(android.content.Context) 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) Toast(android.widget.Toast) RequestQueue(com.android.volley.RequestQueue) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest)

Aggregations

Response (com.android.volley.Response)111 VolleyError (com.android.volley.VolleyError)109 JSONObject (org.json.JSONObject)81 JsonObjectRequest (com.android.volley.toolbox.JsonObjectRequest)61 RequestQueue (com.android.volley.RequestQueue)59 HashMap (java.util.HashMap)58 JSONException (org.json.JSONException)58 User (model.User)50 JSONArray (org.json.JSONArray)29 Context (android.content.Context)18 Toast (android.widget.Toast)18 CustomRequestArray (dz.easy.androidclient.Util.CustomRequestArray)18 StringRequest (com.android.volley.toolbox.StringRequest)14 TextView (android.widget.TextView)10 CustomRequest (dz.easy.androidclient.Util.CustomRequest)9 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)7 Intent (android.content.Intent)6 GridLayoutManager (android.support.v7.widget.GridLayoutManager)6 JsonArrayRequest (com.android.volley.toolbox.JsonArrayRequest)6 MaterialViewPagerHeaderDecorator (com.github.florent37.materialviewpager.header.MaterialViewPagerHeaderDecorator)5