gpt4 book ai didi

java - 如何获取android中对象的字段值

转载 作者:行者123 更新时间:2023-12-02 12:39:05 25 4
gpt4 key购买 nike

我是安卓新手。我正在开发一个简单的 android 项目,其中使用 AsyncTaskonPostExecute 方法,在该方法中我从参数中的 doInBackground() 获取结果结果

    @Override
protected void onPostExecute(Object result) {
// dismiss the dialog once product deleted
//Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG).show();

try {
Log.d(result.toString(),"resultvalue");
if (result != null) {

Toast.makeText(getApplicationContext(),result.message, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Unable to retrieve any data from server", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}


}

当我将结果记录为字符串时,我得到了值

   {"success":1,"message":"Successfully registered the user","error_code":"200"}

但我想显示 message 中的值,但无法从对象 result 中获取它。有人能帮我吗。我被这个问题困扰了。

最佳答案

您得到的响应是一个 JSONObject。您应该使用该字符串创建一个 JSONObject 并获取您需要的值。

JSONObject jsonObject = new JSONObject(result.toString());
jsonObject.getInt("success");
jsonObject.getString("message");
jsonObject.getInt("error_code");

PS:你不应该那样使用Log.d。名称应该是第一个参数而不是第二个,值应该是第二个而不是第一个。

关于java - 如何获取android中对象的字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45007655/

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