gpt4 book ai didi

java - getBaseContext() 解析数据时出错

转载 作者:行者123 更新时间:2023-11-30 10:55:00 24 4
gpt4 key购买 nike

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

最佳答案

.My code keeps giving me errors under my getBaseContext() code at the end and I have no idea why as I think it looks ok!!!

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com