Json解析在Android中返回空数组

时间:2014-07-24 14:40:22

标签: java android arrays json android-arrayadapter

我正在尝试解析json并在数组中返回它的值,然后我想在listview中显示它,日志显示我想要的结果,但它没有在我的手机中显示,我'我错过了一些我不知道的事情。

制作SyncTask的功能:

   public class SincronizarTask {
    private Activity ctx;
    public SincronizarTask(Activity applicationContext) {
        this.ctx = applicationContext;
    }

    public void inicia(){
        this.syncClientes();
    }

    private void syncClientes() {
        ClientRest mClientesRest = new ClientRest(ctx);
        mClientesRest.getClientes(new ClientRest.ClientesRestListener() {
            public void clientesReceived(List<ClientesModel> clientes) {
                for (int i = 0; i < clientes.size(); i++) {
                    System.out.println(clientes.get(i).getNome());
                }
            }
        });
    }
}

ClienteActivity:

public class ClienteActivity extends SherlockFragment {
    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_cliente, container, false);
        try {
            ClienteViewAdapter ad = new ClienteViewAdapter(this.getActivity(), R.layout.fragment_cliente_item);
            ListView lv = (ListView) view.findViewById(R.id.listaClientes);

            lv.setVerticalFadingEdgeEnabled(true);
            lv.setVerticalScrollBarEnabled(true);

            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                   // TextView clienteId = (TextView) view.findViewById(R.id.clienteId);
                    TextView clienteNome = (TextView) view.findViewById(R.id.clienteNome);
                    TextView clienteTipoPessoa = (TextView) view.findViewById(R.id.clienteTipo);

                    Intent intent = new Intent(getActivity(), ContatosView.class);
                    //intent.putExtra("id_cliente", clienteId.getText()); // envia o id do cliente para a tela de detalhes
                    intent.putExtra("nome", clienteNome.getText());
                    intent.putExtra("tipo_pessoa", clienteTipoPessoa.getText());

                    startActivity(intent);
                }
            });

            lv.setAdapter(ad);
        } catch (Exception e) {
            new AlertDialog.Builder(getActivity()).setTitle("Erro").setMessage(e.getMessage()).setNeutralButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                }
            }).show();
        }
        return view;
    }

ClienteViewAdapter:

public class ClienteViewAdapter extends ArrayAdapter<ClientesModel> {
    private final LayoutInflater inflater;
    private final int resourceId;

    public ClienteViewAdapter(FragmentActivity context, int resource) {
        super(context, resource);
        this.inflater = LayoutInflater.from(context);
        this.resourceId = resource;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ClientesModel cliente = getItem(position);

        view = inflater.inflate(resourceId, parent, false);

        //TextView tvId = (TextView) view.findViewById(R.id.txtTitle);
        TextView tvNome = (TextView) view.findViewById(R.id.clienteNome);
        TextView tvTipo = (TextView) view.findViewById(R.id.clienteTipo);

        //tvId.setText(String.valueOf(cliente.ge()));
        tvNome.setText(cliente.getNome());
        tvTipo.setText(cliente.getTipo_pessoa());

        return view;
    }
}

ClientRest:

        JsonObjectRequest mJsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                List<ClientesModel> clientes = null;
                try {
                    clientes = parseJSON(response);
                    pDialog.hide();
                } catch (JSONException e) {
                    e.printStackTrace();

                }
                listener.clientesReceived(clientes);
            }

        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                if (pDialog.isShowing()) {
                    pDialog.hide();
                }
                error.printStackTrace();
            }

        }
        ) 

        mRequestQueue.add(mJsonObjectRequest);
    }

    private List<ClientesModel> parseJSON(JSONObject json) throws JSONException {
        try {
            JSONArray dados = json.getJSONArray("dados");

            for (int i = 0; i < dados.length(); i++) {
                JSONObject item = dados.getJSONObject(i);

                ClientesModel mClientesModel = new ClientesModel();
                ClientesContatosModel mClientesContatoModel = new ClientesContatosModel();

                /* cadastra os dados necessários no objeto no modelo */
                mClientesModel.setId(item.optInt("id"));
                mClientesModel.setNome(item.optString("nome"));
                mClientesModel.setTipo_pessoa(item.optString("tipo_pessoa"));
                mClientesModel.setInformacoes_adicionais(item.optString("informacoes_adicionais"));
                mClientesModel.setCpf(item.optString("cpf"));
                mClientesModel.setCnpj(item.optString("cnpj"));
                JSONArray contatos = item.getJSONArray("contatos");
                for (int j = 0; j < contatos.length(); j++) {
                    JSONObject data = contatos.getJSONObject(j);

                    mClientesContatoModel.setNome(data.optString("nome"));

                    mClientesContatoModel.setCargo(data.optString("cargo"));

                }

                arrNews.add(mClientesModel);
                arrContatos.add(mClientesContatoModel);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return arrNews;
    }
}

0 个答案:

没有答案