Java 如何在android中获取object中的字段值

Java 如何在android中获取object中的字段值,java,android,Java,Android,我是android新手。我正在从事一个简单的android项目,在这个项目中,我使用AsyncTask的onPostExecute方法,从参数result中的doInBackground获取结果 当我将结果记录为字符串时,我得到的是值 {"success":1,"message":"Successfully registered the user","error_code":"200"} 但我想在消息中显示该值,但无法从对象结果中获取该值。有人能帮我吗。我被这个问题困住了。解析JSON,

我是android新手。我正在从事一个简单的android项目,在这个项目中,我使用AsyncTask的onPostExecute方法,从参数result中的doInBackground获取结果

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

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

但我想在消息中显示该值,但无法从对象结果中获取该值。有人能帮我吗。我被这个问题困住了。

解析JSON,然后显示它:

try {
    // Log.d(result.toString(), "resultvalue");
    if (result != null) {
        // ADDED CODE IS HERE:
        JSONObject json = new JSONObject(result.toString());
        String message = json.getString("message");
        Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(getApplicationContext(),
            "Unable to retrieve any data from server", Toast.LENGTH_LONG).show();
    }
} catch (JSONException e) {
    // YOU MAY WANT TO MAKE A TOAST HERE AS WELL
    e.printStackTrace();
}

为了更彻底,如果在解析消息时发生异常,您可能还希望向用户发出某种错误提示。

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

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

附言:你不应该使用日志。我喜欢这样。名称应该是第一个参数而不是第二个,值应该是第二个而不是第一个。

您需要将结果解析为JsonObject


最好始终使用通用异常处理程序,这样,如果除了预期的异常之外还有任何其他异常,则应用程序不会突然停止,并且您可以向用户显示适当的用户友好消息,并根据您的应用程序需求在文件或数据库中进行内部处理,以收集报告用户同意等

Log.d的第一个参数始终是一个标记,可用于搜索logcat。我使用的标签,你可以使用任何适合你,使它更容易搜索日志

@Override
protected void onPostExecute(Object result) {
    // dismiss the dialog once product deleted
    //Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG).show();
    try {
        if (result != null) {
            // display result.toString() only after checking is null or not
            Log.d("TAG", result.toString());
            if  (result.toString().length() > 0) {
                JSONObject json = new JSONObject(result.toString());
                if (json != null && json.getString("message") != null) {
                     String message = json.getString("message");
                     Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
                } else {
                    // TOAST for empty result or capture this for future analysis
                }
            } else {
                // TOAST for empty result or capture this for future analysis
            }
        } else {
            Toast.makeText(getApplicationContext(), "Unable to retrieve any data from server", Toast.LENGTH_LONG).show();
        }
    } catch (JSONException e) {
        // Have a toast to display user friendly message
        // Capture this in files or DB so can be used to report on user consent
        Log.d("TAG", "Exception Details: " + e.toString());
        e.printStackTrace();
    } catch (Exception e) {
        // Have a toast to display user friendly message
        // Capture this in files or DB so can be used to report on user consent
        Log.d("TAG", "Exception Details: " + e.toString());
        e.printStackTrace();
    }
}

您应该解析它您确定响应将始终是json吗
@Override
    protected void onPostExecute(Object result) {
        try {
            Log.d(result.toString(),"resultvalue");
            JSONObject jsonObject = new JSONObject(result.toString());
            if (jsonObject.getInt("success")==1) {
                Toast.makeText(getApplicationContext(),jsonObject.getString("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();
        }
    }
@Override
protected void onPostExecute(Object result) {
    // dismiss the dialog once product deleted
    //Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG).show();
    try {
        if (result != null) {
            // display result.toString() only after checking is null or not
            Log.d("TAG", result.toString());
            if  (result.toString().length() > 0) {
                JSONObject json = new JSONObject(result.toString());
                if (json != null && json.getString("message") != null) {
                     String message = json.getString("message");
                     Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
                } else {
                    // TOAST for empty result or capture this for future analysis
                }
            } else {
                // TOAST for empty result or capture this for future analysis
            }
        } else {
            Toast.makeText(getApplicationContext(), "Unable to retrieve any data from server", Toast.LENGTH_LONG).show();
        }
    } catch (JSONException e) {
        // Have a toast to display user friendly message
        // Capture this in files or DB so can be used to report on user consent
        Log.d("TAG", "Exception Details: " + e.toString());
        e.printStackTrace();
    } catch (Exception e) {
        // Have a toast to display user friendly message
        // Capture this in files or DB so can be used to report on user consent
        Log.d("TAG", "Exception Details: " + e.toString());
        e.printStackTrace();
    }
}