解析数据时出现getBaseContext()错误

时间:2015-11-01 14:17:36

标签: java android error-handling

此代码用于尝试registation.class用于解析数据的android studio应用程序。我的代码一直在我的getBaseContext()代码下给我错误,我不知道为什么我觉得它看起来没问题!代码是针对实验室的,所以它应该是正确的,但我继续得到错误!!!!

谁能告诉我?任何帮助将非常感激!谢谢

 public class AttemptRegistration extends
    AsyncTask<String, Integer, String> {

int success;
String message = " ";

@Override
protected String doInBackground(String... args) {
    try {
        Map<String, String> params = new HashMap<String, String>();
        params.put("tag", "register");
        params.put("username", args[0]);
        params.put("password", args[1]);
        params.put("email", args[2]);

        //
        HttpUtility.sendPostRequest(params);

        //


        String response = HttpUtility.readRespone();

        JSONObject jObj = null;

        try {

            jObj = new JSONObject(response);

            success = jObj.getInt("success");
            message = jObj.getString("message");


        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data" + e.toString());
        }
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        HttpUtility.disconnect();
        return message;

}
    protected void onPostExecute(String status) {

    if (status !=null) {

        Toast.makeText(getBaseContext(), status, Toast.LENGTH_LONG).show();

        if(success == 1) {
            startActivity (new Intent(getBaseContext(), LoginActivity.class));
        }
    }

}

1 个答案:

答案 0 :(得分:1)

  

。我的代码一直在我的getBaseContext()代码下给我错误   结束,我不知道为什么我觉得它看起来不错!!!

getBaseContext()ContextWrapper的一种方法。由于您收到cannot resolve the method错误,这意味着您的AsyncTask类未定义为继承自ContextWrapperActivity E.g)的类的内部类。您可以将Context传递给AsyncTask

public class AttemptRegistration extends AsyncTask<String, Integer, String> {
    private final Context mContext;
    public AttemptRegistration(Context context) {
       mContext = context;
    }

然后使用mContext代替getBaseContext()