How to get a return item from a json encode

0

I would like to know how to store each separate item so that I can then use it in a 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;

    }
}

The return of her are 4 items, until then ok.

After this, I want to save each item of this 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 from a select in web service php that returns a json encode.

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
}
    
asked by anonymous 13.04.2016 / 15:15

1 answer

1

I do not know if 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
}
    
13.04.2016 / 16:34