0
I need to assign in Edittext fields, from my form, the data of an object. For example, I’m using an API that searches the user’s address for viaCep. I’m already able to return the data, now I need to throw this data in its proper fields.
public void getCEP(String cep){
RequestParams rp = new RequestParams();
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://viacep.com.br/ws/" + cep + "/json/", rp, new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
Toast.makeText(getBaseContext(), "Problema na conexao!"+statusCode, Toast.LENGTH_LONG).show();
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
try {
JSONObject obj = new JSONObject(responseString);
String retorno = "";
if (!obj.has("erro")) {
retorno += "\n" + obj.getString("cep");
retorno += "\n" + obj.getString("logradouro");
retorno += "\n" + obj.getString("complemento");
retorno += "\n" + obj.getString("bairro");
retorno += "\n" + obj.getString("localidade");
retorno += "\n" + obj.getString("uf");
retorno += "\n" + obj.getString("ibge");
retorno += "\n" + obj.getString("gia");
Toast.makeText(getBaseContext(),"Dados retornados: "+retorno, Toast.LENGTH_LONG).show();
EditText edtCep = ((EditText) findViewById(R.id.edtCep));
//edtCep.setText("cep", retorno.);
}
} catch (JSONException e){
}
}
});
}
public void btnCadastrar(View view) {
final EditText edtNome = ((EditText) findViewById(R.id.edtNome));
final EditText edtSobrenome = ((EditText) findViewById(R.id.edtSobrenome));
EditText edtCelular = ((EditText) findViewById(R.id.edtCelular));
EditText edtCep = ((EditText) findViewById(R.id.edtCep));
final String nome = edtNome.getText().toString();
String sobrenome = edtSobrenome.getText().toString();
String celular = edtCelular.getText().toString();
String cep = edtCep.getText().toString();
String url = "http://reservacomdomanda.com/areaAdmin/api/area_admin/usuario.php";
StringRequest srUrl = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
})
{
protected Map<String, String > getParams(){
Map<String, String> params = new HashMap<String, String>();
params.put("nome", edtNome.getText().toString());
params.put("sobrenome", edtSobrenome.getText().toString());
return params;
}
};
}
Right, but where’s the zip code from inside the return object, on that command of yours? return.zip, something like that
– GustavoSevero
Uses "obj.getString("cep")" instead of "return"
– Márcio Oliveira
Okay, I’m gonna try...
– GustavoSevero
If you are not sure that "obj" data can contain data, use optString instead of getString, with this Edittext will simply become empty instead of generating an exception.
– Márcio Oliveira
Yeah, what you said before, it didn’t work out so well, because I gave a Lod.d... and returned this on the console: "D/CEP: android.support.v7.widget.Appcompatedittext{1b611dab VFED.. CL ..."
– GustavoSevero
I did this: edtCep.setText(obj.optString("cep"), Textview.BufferType.EDITABLE); Log. d("CEP", String.valueOf(edtCep));
– GustavoSevero
The cep may not be coming in your json. In your "return" string it appears?
– Márcio Oliveira
Yes, I have a Toast that shows all the data that comes in the return object. Here: Toast.makeText(getBaseContext(),"Returned Data: "+return, Toast.LENGTH_LONG). show();
– GustavoSevero
Yes, I noticed this Oast. But it’s strange that the cep appears in it and not in editText, since they both use the same obj.getString("cep").
– Márcio Oliveira
Ta, forget what I told you before kkkkk It is that as I already fill the ZIP code to fetch the data, it is obvious that it will not reappear... I did the same with patio and he appeared.... THANKS.
– GustavoSevero