这段代码用于尝试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));
        }
    }

}

最佳答案



getBaseContext()ContextWrapper的方法。由于收到cannot resolve the method错误,这意味着您的AsyncTask类未定义为从ContextWrapper(Activity例如)继承的类的内部类。您可以将Context传递给AsyncTask

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

然后使用mContext而不是getBaseContext()

关于java - 解析数据的getBaseContext()错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33463135/

10-10 06:31