gpt4 book ai didi

Java - 获取 JSONObject 时出现 NullPointerException

转载 作者:太空狗 更新时间:2023-10-29 15:38:55 25 4
gpt4 key购买 nike

我正在尝试使用 Lukencode 在我的 Android 应用程序中实现登录功能的 RestClient 类。我有一个名为 UserFunctions 的类,用于通过 RestClient 检索 JSONObject:

public class UserFunctions {

private RestClient restClient;
private String json;
private Context mContext;

private static String login_tag = "login";
private static String register_tag = "register";

// constructor
public UserFunctions(Context context){
restClient = new RestClient("http://10.0.2.2:81/HGourmet/user");
mContext = context;
}

/**
* function make Login Request
* @param email
* @param password
* */
public JSONObject loginUser(String email, String password){
// Building Parameters
restClient.AddParam("tag", login_tag);
restClient.AddParam("email", email);
restClient.AddParam("password", password);

// getting JSON Object
try{
restClient.Execute(RequestMethod.POST);
}catch(Exception e){
e.printStackTrace();
}
json = restClient.getResponse();

JSONObject jObj = null;
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}

下面是我在 logcat 中得到的内容:

01-06 09:01:05.805: E/Trace(1819): error opening trace file: No such file or directory (2)
01-06 09:02:05.485: E/AndroidRuntime(1819): FATAL EXCEPTION: main
01-06 09:02:05.485: E/AndroidRuntime(1819): java.lang.NullPointerException
01-06 09:02:05.485: E/AndroidRuntime(1819): at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
01-06 09:02:05.485: E/AndroidRuntime(1819): at org.json.JSONTokener.nextValue(JSONTokener.java:94)
01-06 09:02:05.485: E/AndroidRuntime(1819): at org.json.JSONObject.<init>(JSONObject.java:154)
01-06 09:02:05.485: E/AndroidRuntime(1819): at org.json.JSONObject.<init>(JSONObject.java:171)
01-06 09:02:05.485: E/AndroidRuntime(1819): at com.hanu.hgourmet.library.UserFunctions.loginUser(UserFunctions.java:49)
01-06 09:02:05.485: E/AndroidRuntime(1819): at com.hanu.hgourmet.LoginActivity$1.onClick(LoginActivity.java:56)
01-06 09:02:05.485: E/AndroidRuntime(1819): at android.view.View.performClick(View.java:4202)
01-06 09:02:05.485: E/AndroidRuntime(1819): at android.view.View$PerformClick.run(View.java:17340)
01-06 09:02:05.485: E/AndroidRuntime(1819): at android.os.Handler.handleCallback(Handler.java:725)
01-06 09:02:05.485: E/AndroidRuntime(1819): at android.os.Handler.dispatchMessage(Handler.java:92)
01-06 09:02:05.485: E/AndroidRuntime(1819): at android.os.Looper.loop(Looper.java:137)
01-06 09:02:05.485: E/AndroidRuntime(1819): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-06 09:02:05.485: E/AndroidRuntime(1819): at java.lang.reflect.Method.invokeNative(Native Method)
01-06 09:02:05.485: E/AndroidRuntime(1819): at java.lang.reflect.Method.invoke(Method.java:511)
01-06 09:02:05.485: E/AndroidRuntime(1819): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-06 09:02:05.485: E/AndroidRuntime(1819): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-06 09:02:05.485: E/AndroidRuntime(1819): at dalvik.system.NativeStart.main(Native Method)

我真的希望有人能帮我查明这个问题的原因。非常感谢。

最佳答案

我不会详细说明您应该如何执行此操作,而只会详细说明它发生的原因。根据定义,AsyncTask 是异步的。这意味着当你执行它时,它会在不同的线程中并行完成它的工作。所以你不能期望它在 execute() 被调用后立即终止(否则它会被称为 SyncTask,并且没有任何理由存在) .

AsyncTask 视为 toastr 。当您启动 toastr (执行 AsyncTask)时,它会 toast ,但不会立即返回烤好的面包。烤它需要一些时间。当它正在 toast 时,您可以做其他事情(这样就不会完全卡住 UI 线程)。当它烤完你的面包时,它会发出一声 叮! 通知你面包已经烤好了。这里也是一样的。当 AsyncTask 通知您它已完成执行时,您应该只开始使用 json( toast )。

关于Java - 获取 JSONObject 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14180643/

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