How to pick up an item from the return of a json

Asked

Viewed 220 times

0

I would like to know how to store each separate item so I can use it later in one sharedpreferences.

There is this class in my program ....

public class Tab {


    private String UC;
    private String Instalacao;
    private String Nome_Logo;
    private String Tensao;

    public String getUC() {
        return UC;
    }

    public void setUC(String UC) {
        this.UC = UC;
    }

    public String getInstalacao() {
        return Instalacao;
    }

    public void setInstalacao(String instalacao) {
        Instalacao = instalacao;
    }

    public String getNome_Logo() {
        return Nome_Logo;
    }

    public void setNome_Logo(String nome_Logo) {
        Nome_Logo = nome_Logo;
    }

    public String getTensao() {
        return Tensao;
    }

    public void setTensao(String tensao) {
        Tensao = tensao;
    }
    @Override
    public String toString()
    {
        return Tensao + "  " + Nome_Logo + "  " + UC + "   " + Instalacao;

    }
}

Her return is 4 items, until then ok.

After that, I want to save each item of that into a separate variable for me to use later (I’ll use sharedpreferences to store each variable).

And here I transform this table which is a return of a select in php web service which returns a json encounter.

public class Empresa extends AppCompatActivity {

    Button btnfatura;
    private String jsonResult;
    private String caminho = "http://locahost/abobora.php?aa=par1&bb=par2";
    private ListView unidade;
    private ArrayList<Tab> lista;
    private String caminho2 = "";
    private String Codigo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_empresa);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        btnfatura = (Button) findViewById(R.id.btnfatura);
        String filial = getIntent().getExtras().getString("Filial");
        Toast.makeText(getApplicationContext(), "" + filial, Toast.LENGTH_SHORT).show();


        caminho2 = caminho;
        caminho2 = caminho2.replace("par1",Preferencia.getCodEmpresa(this));
        caminho2 = caminho2.replace("par2",filial.toString());
        Log.i("TESTE", "" + caminho2);
        accessWebService();
    }

    private class JsonReadTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(params[0]);
            try {
                HttpResponse response = httpclient.execute(httppost);
                jsonResult = inputStreamToString(
                        response.getEntity().getContent()).toString();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return jsonResult.toString();
        }

        private StringBuilder inputStreamToString(InputStream is) {
            String rLine = "";
            StringBuilder answer = new StringBuilder();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));

            try {
                while ((rLine = rd.readLine()) != null) {
                    answer.append(rLine);
                }
            } catch (IOException e) {
                //e.printStackTrace();
                Toast.makeText(getApplicationContext(),
                        "Error..." + e.toString(), Toast.LENGTH_LONG).show();
            }
            return answer;
        }// end inputstream

        @Override
        protected void onPostExecute(String result) {
            Log.i("TESTE", "" + result);

            //transformando em objeto
            Gson gson = new Gson();
            Type listType = new TypeToken<ArrayList<Tab>>(){}.getType();
            lista = gson.fromJson(result, listType);

               e qui que pego aquela lista  e é aqui que quero armazenar cada item daquela lista separadamente... como eu faço isso 
obrigado
               estou tentando armazenar desse jeito porem esta errado...
            //Codigo  = gson.fromJson(listType[1]);

            Log.i("QTDE de itens", "" + lista.size());
            unidade = (ListView) findViewById(R.id.unidade);

            final ArrayAdapter<Tab> adapter = new ArrayAdapter<Tab>
                    (Empresa.this, android.R.layout.simple_list_item_1, lista);
            unidade.setAdapter(adapter);

            btnfatura.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent in = new Intent(Empresa.this, Filiais.class);
                    startActivity(in);
                }
            });
        }
    }
    public void accessWebService() {
        JsonReadTask task = new JsonReadTask();
        // passes values for the urls string array
        task.execute(new String[]{caminho2});
    }//end acesso web
}

1 answer

1

I’m not sure I understand exactly what you want to do but maybe you can use a foreach to go through the "list" array and retrieve item by item.

for (String item : lista) {
    // faça algo
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.