此代码用于尝试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));
}
}
}
答案 0 :(得分:1)
。我的代码一直在我的getBaseContext()代码下给我错误 结束,我不知道为什么我觉得它看起来不错!!!
getBaseContext()是ContextWrapper
的一种方法。由于您收到cannot resolve the method
错误,这意味着您的AsyncTask
类未定义为继承自ContextWrapper
(Activity
E.g)的类的内部类。您可以将Context
传递给AsyncTask
。
public class AttemptRegistration extends AsyncTask<String, Integer, String> {
private final Context mContext;
public AttemptRegistration(Context context) {
mContext = context;
}
然后使用mContext
代替getBaseContext()