没有在片段android上获得JSON解析结果

时间:2015-10-29 03:26:25

标签: java android json parsing

我想让JSON填充edittext,我写的片段java就像这样:

public class banktransferfragment extends Fragment {
Intent intent;
JSONObject jsonobject;
JSONArray jsonarray;
ArrayList<String> banktransferlist;
ArrayList<BankTransferModel> banktransfer;
EditText editText_BankAccountName, editText_BankAccountNumber, editText_BankName, editText_csemail, editText_csphone;
private static final String LOG_TAG = "BankTransfer";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_bank_transfer, container, false);
    banktransfer = new ArrayList<BankTransferModel>();
    banktransferlist = new ArrayList<String>();
    Bundle extras = getActivity().getIntent().getExtras();
    final String token= extras.getString("TOKEN");
    final String checkbox_state= extras.getString("checkbox_state");
    editText_BankAccountName = (EditText) rootView.findViewById(R.id.editText_BankAccountName);
    editText_BankAccountNumber = (EditText) rootView.findViewById(R.id.editText_BankAccountNumber);
    editText_BankName = (EditText) rootView.findViewById(R.id.editText_BankName);
    editText_csemail = (EditText) rootView.findViewById(R.id.editText_csemail);
    editText_csphone = (EditText) rootView.findViewById(R.id.editText_csphone);
    // JSON file URL address
    SendfeedbackJob job = new SendfeedbackJob();
    job.execute();
    intent = new Intent(getActivity(), LegalNoticePage.class);
    intent.putExtra("checkbox_state", checkbox_state);
    intent.putExtra("TOKEN", token);
    final Button button = (Button) rootView.findViewById(R.id.transaction_button);

    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            startActivity(intent);
        }
    });
    return rootView;
}
private class SendfeedbackJob extends AsyncTask<String, Void, String> {
    private static final String LOG_TAG = "BankTransfer";
    Bundle extras = getActivity().getIntent().getExtras();
    final String token= extras.getString("TOKEN");
    final String checkbox_state= extras.getString("checkbox_state");
    @Override
    protected String doInBackground(String... params) {
        final String url_bank_transfer = Constant.URI_BASE_CONFIG + "?token=" + token;
        Utils.log("url_bank_transfer: " + url_bank_transfer);
        try{
            // Locate the NodeList name
            HttpGet httppost = new HttpGet(url_bank_transfer);
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            String data = EntityUtils.toString(entity);
            Utils.log("data transfer: " + data);
            JSONArray json = new JSONArray(data);
            for (int i = 0; i < json.length(); i++) {
                jsonobject = json.getJSONObject(i);
                final String BankAccountName=jsonobject.getString("bank_account_name");
                final String BankAccountNumber=jsonobject.getString("bank_account_number");
                final String BankName=jsonobject.getString("bank_name");
                final String csemail=jsonobject.getString("cs_email");
                final String csphone=jsonobject.getString("cs_phone");
                Utils.log("BankAccountName: " + BankAccountName);

                getActivity().runOnUiThread(new Runnable() {

                    public void run() {
                        editText_BankAccountName.setText(BankAccountName);
                        editText_BankAccountNumber.setText(BankAccountNumber);
                        editText_BankName.setText(BankName);
                        editText_csemail.setText(csemail);
                        editText_csphone.setText(csphone);
                    }
                });
            }
        }catch (Exception e) {
            Log.e(LOG_TAG, String.format("Error during bank transfer: %s", e.getMessage()));
            //e.printStackTrace();
        }
        return "processing";
    }
    protected void onPostExecute(String message) {
        //process onPostExecute
    }
}
}

更新: 我得到了Utils.log("url_bank_transfer: " + url_bank_transfer); and Utils.log("data transfer: " + data);

[{"site_tittle":"Book My Fit","global_class_fee":"50000","bank_account_name":"Book My Fit","bank_account_number":"xxxxxxxxxx","bank_name":"BCA Cabang Permata Hijau","cs_email":"cs@bookmyfit.com","cs_phone":"+62215135143","sandbox_vt_client_key":"VT-client-OnqFmrAF1lmDR_aI","sandbox_vt_server_key":"VT-server-0T91A-yygrsiaJdZtq591H7s","production_vt_client_key":"not-available","production_vt_server_key":"not-available","footer_1":"about","footer_2":"learn more","footer_3":"partners","footer_4":"connect","site_description_en":"BookMyFit.com is your new fitness partner that allows you a monthly membership to hundreds of gyms, fitness studios and activities in your city, so you can try something new every day. Download the mobile app & get ready for your fitness journey.","site_description_id":"BookMyFit.com adalah partner fitness terbaru yang memungkinkan anda mendapatkan membership bulanan ke ratusan gym, studio fitness dan aktivitas dikota anda, sehingga anda bisa mencoba sesuatu yang baru setiap harinya. Download aplikasi untuk mobile dan be","office_address":"370, Palma One Building<br \/> Jl. H.R. Rasuna Said Kav. X-2\/4<br \/> Jakarta 12950 - Indonesia<br \/> +62 21 5228775"}]

如何纠正这种情况,以便在edittext上显示文字?

更新:似乎JSON中的更改变为JSONArray才能使其正常运行。问题已经结束。

1 个答案:

答案 0 :(得分:0)

您将data作为一个整体JSONObject而不是JSONArray。因此,您必须将data放入JSONObject。试试这样。

                JSONObject jsonobject= new JSONObject(data);        
                String BankAccountName=jsonobject.getString("bank_account_name");
                String BankAccountNumber=jsonobject.getString("bank_account_number");
                String BankName=jsonobject.getString("bank_name");
                String csemail=jsonobject.getString("cs_email");
                String csphone=jsonobject.getString("cs_phone");