具有两个JSONClasses的活动

时间:2014-07-07 21:22:10

标签: android json

我有一个活动,假设两次使用JSON。在第一次它完美地工作,但第二次似乎JSON类直接转到onPostExecute函数而不激活doInBackground函数。这是调用JSON的活动:

public class Registration extends ActionBarActivity {

    public void regis(View view) {
        EditText userName = (EditText) findViewById(R.id.userNameTxt);
        EditText pass1 = (EditText) findViewById(R.id.PassTxt);
        EditText pass2 = (EditText) findViewById(R.id.RePassTxt);
        EditText displayName = (EditText) findViewById(R.id.DisplayTxt);
        EditText mail = (EditText) findViewById(R.id.eMailTxt);
        CheckBox agree = (CheckBox) findViewById(R.id.checkAgree);
        TextView err = (TextView) findViewById(R.id.err);

        String uName = userName.getText().toString(), pss1 = pass1.getText().toString(),
                pss2 = pass2.getText().toString(), disName = displayName.getText().toString(),
                eMail = mail.getText().toString();

        if ((uName.length() > 0) && (pss1.length() > 0) && (pss2.length() > 0) && (disName.length() > 0)
                && (eMail.length() > 0)) {
            if (pss1.equals(pss2)) {
                if (agree.isChecked()) {
                    err.setVisibility(TextView.INVISIBLE);
                    String str = "?uname=" + uName;
                    new JSONClass(this, 2, str, this).execute();
                } else {
                    err.setText("You must approve the use terms");
                    err.setVisibility(TextView.VISIBLE);
                }
            } else {
                err.setText("The two passwords must match!");
                err.setVisibility(TextView.VISIBLE);
            }
        } else {
            err.setText("You must insert values in every cell");
            err.setVisibility(TextView.VISIBLE);
        }
    }

    public void checkExist(JSONArray ans) {
        try {
            int cnt = Integer.parseInt(ans.getJSONObject(0).getString("cnt"));

            if (cnt == 1) {
                TextView err = (TextView) findViewById(R.id.err);
                err.setText("User name already exists!");
                err.setVisibility(TextView.VISIBLE);
            } else {
                EditText userName = (EditText) findViewById(R.id.userNameTxt);
                EditText pass = (EditText) findViewById(R.id.PassTxt);
                EditText displayName = (EditText) findViewById(R.id.DisplayTxt);
                EditText mail = (EditText) findViewById(R.id.eMailTxt);

                String uName = userName.getText().toString(), pss = pass.getText().toString(),
                        disName = displayName.getText().toString(), eMail = mail.getText().toString();

                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss yyyy-MM-dd");
                String currentDateandTime = sdf.format(new Date());

                String str = "?uname=" + uName + "&password=" + pss + "&email=" + eMail + "&disnam=" +
                        disName + "&dt=" + currentDateandTime;

                Log.e("log_tag", "Just for me: " + str);

                new JSONClass(this, 3, str, this).execute();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void openLogin() {
        Intent intent = new Intent(this, Login.class);
        startActivity(intent);
        finish();
    }
}

我删除了不必要的功能。这是我的JSON类:

public class JSONClass extends AsyncTask<String,Void,JSONArray>{

    private String link;
    private int flag;
    private Login log = null;
    private Registration reg = null;
    private Context context;

    public JSONClass(Context context, int queryNo, String params, Registration regg) {
        this.context = context;
        link = "http://pickupfriend.fulba.com/android_project/query" + queryNo + ".php" + params;
        reg = regg;
        flag = queryNo;
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    @Override
    protected JSONArray doInBackground(String... arg0) {
        JSONArray arr = new JSONArray();
        try{
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(link);
            post.addHeader("Content-Type", "application/x-www-form-urlencoded");

            HttpResponse response = client.execute(post);
            HttpEntity entity = response.getEntity();
            String result = EntityUtils.toString(entity, "utf-8");
            arr = new JSONArray(result);

        }catch(Exception e){

        }
        return arr;
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    public void onPostExecute(JSONArray result) {
        switch (flag) {
            case 1:
                log.getResults(result);
                break;
            case 2:
                reg.checkExist(result);
                break;
            case 3:
                reg.openLogin();
                break;
            default:
                break;
        }
    }
}

首先,我激活了与json类完美配合的regis函数。之后如果cnt参数为0,则假设激活checkExist,然后再次激活json类,但它似乎直接进入openLogin函数而不进入php文件并激活它。我做错了吗?

0 个答案:

没有答案