如何从Web Service动态获取值

时间:2014-06-05 09:45:08

标签: android json web-services response

我是JSON概念的新手

在下面的代码中," AgentId"的值和"键入"正在改变基于代理商,但我

持续如下

jsonReq.put("agentsId", 7); and 

jsonReq.put("types", 0);

所以我不得不经常改变它们。怎么弄它请帮帮我

public class Busi_perfo_infor_Activity extends BaseActivity

 {

    private ProgressDialog dialog;


    private TableLayout tableLayout;

    private Handler handler = new Handler();

    private Spinner Month_Spinner;
private List<Indi_Busi_Agent_perfo> agentsList = new ArrayList<Indi_Busi_Agent_perfo>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.busi_perfo_info);
    Month_Spinner = (Spinner) findViewById(R.id.Current_prvious_month_spinner);

    setupHeader();

    tableLayout = (TableLayout) findViewById(R.id.Table_busi_perfo);

    dialog = new ProgressDialog(this);
    dialog.setMessage("Loading...");
    dialog.setIndeterminate(true);
    dialog.setCancelable(true);

    setupBranchSpinner();

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, new String[] { "Previous Month",
                    "Current Month" }) {
    };
    dataAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    Month_Spinner.setAdapter(dataAdapter);
    Month_Spinner
            .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int pos, long arg3) {
                    updateCollectons();
                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                    // TODO Auto-generated method stub

                }
            });
}

private void setupBranchSpinner() {

    dialog.show();

    new Thread() {
        public void run() {
            handler.post(new Runnable() {

                @Override
                public void run() {
                    dialog.dismiss();

                }
            });
        }
    }.start();

}

protected void updateCollectons() {
    // TODO Auto-generated method stub
    dialog.show();
    tableLayout.setVisibility(View.GONE);
    final JSONObject json = new JSONObject();

    try {
        JSONObject jsonReq = new JSONObject(Utilities.loadPref(
                getApplicationContext(), "Login", ""));
        jsonReq.put("agentsId", 7);

        // jsonReq.put("Area", 0);
        // jsonReq.put("Mode", 2);

        SimpleDateFormat fmtOut = new SimpleDateFormat("dd MMM yyyy");
        // dd MMM yyyy
        String dateFormat = fmtOut.format(new Date());
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DATE, 1);

        if (Month_Spinner.getSelectedItemPosition() == 0) {

            cal.add(Calendar.MONTH, -1);
        }
        String fromdateFormat = fmtOut.format(cal.getTime());
        //
        jsonReq.put("FromDate", fromdateFormat);
        jsonReq.put("ToDate", dateFormat);
        jsonReq.put("types", 0);

        json.put("AgntPerformanceRequest", jsonReq);

        new Thread() {
            private Indi_Busi_Agent_perfo info;

            public void run() {
                String response = WebServices.postRequest(
                        WebServices.INDIVI_BUSI_PERFO_URL, json,
                        Busi_perfo_infor_Activity.this);

                if (response != null) {

                    try {
                        JSONObject jsonResp = new JSONObject(response);
                        if (jsonResp
                                .has("AgntPerformanceResult")) {
                            info = new Indi_Busi_Agent_perfo(
                                    jsonResp.getJSONObject(
                                            "AgntPerformanceResult"));

                        } else {
                            Toast.makeText(getApplicationContext(),
                                    "their are no subscribers",
                                    Toast.LENGTH_LONG).show();
                        }
                    }

                    catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                handler.post(new Runnable() {

                    @Override
                    public void run() {
                        dialog.dismiss();
                        if (info != null) {
                            updateCollectionTableView(info);
                        }
                    }

                });

            }
        }.start();

    } catch (JSONException e) {
        e.printStackTrace();
    }
}

private void updateCollectionTableView(Indi_Busi_Agent_perfo info) {

    tableLayout.setVisibility(View.VISIBLE);

    ((TextView) findViewById(R.id.bch_name)).setText(Utilities
            .getString(info.getBranchName()));

    ((TextView) findViewById(R.id.busi_Amt)).setText(Utilities
            .getDecimalFormat(info.getBusinessAmt()));

}

}

任何帮助都会给我一个建议 谢谢。

0 个答案:

没有答案