Search in sources :

Example 1 with Aplicativo

use of model.Aplicativo in project aplicativo by InCasa.

the class FragmentCelular method GetConfig.

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

        @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() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getActivity(), "Configuração Celular: Erro!", Toast.LENGTH_SHORT).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;
        }
    };
    // Add the request to the RequestQueue.
    //fila de requisições
    RequestQueue fila = Volley.newRequestQueue(getActivity().getApplicationContext());
    //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 2 with Aplicativo

use of model.Aplicativo in project aplicativo by InCasa.

the class FragmentCelular method UpdateConfig.

public void UpdateConfig(JSONObject json, String URLUPDATE) {
    final Aplicativo app = Aplicativo.getInstancia();
    URLUPDATE = URLUPDATE + app.getIdAplicativo();
    JsonObjectRequest req = new JsonObjectRequest(Request.Method.PUT, URLUPDATE, json, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            Toast.makeText(getActivity(), "Configuração Celular: Atualizado com sucesso !", Toast.LENGTH_SHORT).show();
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getActivity(), "Configuração Celular: Erro na atualização !", Toast.LENGTH_SHORT).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;
        }
    };
    // Add the request to the RequestQueue.
    //fila de requisições
    RequestQueue fila = Volley.newRequestQueue(getActivity().getApplicationContext());
    //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) Response(com.android.volley.Response) JSONObject(org.json.JSONObject) RequestQueue(com.android.volley.RequestQueue) Aplicativo(model.Aplicativo) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest)

Example 3 with Aplicativo

use of model.Aplicativo in project aplicativo by InCasa.

the class FragmentCelular method onViewCreated.

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    final EditText macAppField = (EditText) getView().findViewById(R.id.editAppMac);
    final EditText nameAppField = (EditText) getView().findViewById(R.id.editAppName);
    Button btnAppSalvar = (Button) getView().findViewById(R.id.btnAppSalvar);
    Button btnCloneMac = (Button) getView().findViewById(R.id.btnCloneMAC);
    SharedPreferences mSharedPreferences = this.getActivity().getSharedPreferences("ServerAdress", 0);
    String ip = mSharedPreferences.getString("servidor", null);
    final String URLCADASTRO = "http://" + ip + "/backend/aplicativo";
    final String URLGET = "http://" + ip + "/backend/aplicativo/1";
    final String URLUPDATE = "http://" + ip + "/backend/aplicativo/update/";
    GetConfig(URLGET);
    //seta as informações do objeto nos editText
    Aplicativo app = Aplicativo.getInstancia();
    macAppField.setText(app.getMac());
    nameAppField.setText(app.getNome());
    btnCloneMac.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            macAppField.setText(getMacAddr());
        }
    });
    btnAppSalvar.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Aplicativo app = Aplicativo.getInstancia();
            String nome = nameAppField.getText().toString();
            String mac = macAppField.getText().toString();
            if (TextUtils.isEmpty(nome) || TextUtils.isEmpty(mac)) {
                if (TextUtils.isEmpty(nome)) {
                    nameAppField.setError("Preencha o campo nome");
                }
                if (TextUtils.isEmpty(mac)) {
                    macAppField.setError("Preencha o campo MAC");
                }
            } else {
                JSONObject jsonBody = new JSONObject();
                try {
                    jsonBody.put("mac", mac);
                    app.setMac(mac);
                    jsonBody.put("nome", nome);
                    app.setNome(nome);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                //SaveConfig(jsonBody, URLCADASTRO);
                UpdateConfig(jsonBody, URLUPDATE);
            }
        }
    });
}
Also used : EditText(android.widget.EditText) JSONObject(org.json.JSONObject) Button(android.widget.Button) SharedPreferences(android.content.SharedPreferences) Aplicativo(model.Aplicativo) JSONException(org.json.JSONException) View(android.view.View)

Example 4 with Aplicativo

use of model.Aplicativo 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)

Aggregations

Aplicativo (model.Aplicativo)4 JSONObject (org.json.JSONObject)4 RequestQueue (com.android.volley.RequestQueue)3 Response (com.android.volley.Response)3 VolleyError (com.android.volley.VolleyError)3 JsonObjectRequest (com.android.volley.toolbox.JsonObjectRequest)3 HashMap (java.util.HashMap)3 User (model.User)3 JSONException (org.json.JSONException)3 SharedPreferences (android.content.SharedPreferences)1 View (android.view.View)1 Button (android.widget.Button)1 EditText (android.widget.EditText)1