Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当某个对象在应用程序崩溃时未出现时,使用Json获取响应?_Android_Json - Fatal编程技术网

Android 当某个对象在应用程序崩溃时未出现时,使用Json获取响应?

Android 当某个对象在应用程序崩溃时未出现时,使用Json获取响应?,android,json,Android,Json,我正在使用我的响应创建一个模型,创建模型使用JSOn。作为响应,一些参数在某个时间出现,而很少参数在某个时间没有出现,我的代码如下所示。 1.回应 { "result": { "success": true, "data": [ { "uid": "xxxxxxx", "total_activities": "19", "points": 105, "guesstimated_reach": "39", "rank": 1, "bonus_po

我正在使用我的响应创建一个模型,创建模型使用JSOn。作为响应,一些参数在某个时间出现,而很少参数在某个时间没有出现,我的代码如下所示。 1.回应

{
"result": {
"success": true,
"data": [
  {
    "uid": "xxxxxxx",
    "total_activities": "19",
    "points": 105,
    "guesstimated_reach": "39",
    "rank": 1,
    "bonus_points": {
      "details": [],
      "total_bonus_points": 0
    },
    "user_details": {
      "first_name": "Pauras More",
      "last_name": "!",
      "city": "Mumbai",
      "email": "xxxxxxx.com",
      "snetwork_influence": {
        "fb": {
          "friends": 11
        },
        "ln": {
          "connections": 116
        },
        "tw": {
          "friends": 50,
          "followers": 3,
          "listed": 18,
          "favorites": 224,
          "statuses": 1380
        }
      },
      "saccounts": {
        "fb": {
          "id": "xxxxxxx",
          "first_name": "xxxxxxx",
          "last_name": "xxxxxxx",
          "extra_data": {
            "timeline_album_id": "xxxxxxx"
          }
        },
        "ln": {
          "id": "xxxxxxx",
          "first_name": "xxxxxxx",
          "last_name": "xxxxxxx"
        },
        "tw": {
          "id": "xxxxxxx",
          "first_name": "xxxxxxx",
          "last_name": ""
        }
      },
      "pic": "xxxxxxx"
    }
  },
  {
    "uid": "xxxxxxx",
    "total_activities": "21",
    "points": 72,
    "guesstimated_reach": "100",
    "rank": 2,
    "bonus_points": {
      "details": [],
      "total_bonus_points": 0
    },
    "user_details": {
      "first_name": "xxxxxxx",
      "last_name": "T",
      "city": "xxxxxxx",
      "email": "xxxxxxx.com",
      "snetwork_influence": {
        "ln": {
          "connections": 19
        },
        "tw": {
          "friends": 0,
          "followers": 0,
          "listed": 0,
          "favorites": 0,
          "statuses": 0
        },
        "fb": {
          "friends": 527
        }
      },
      "saccounts": {
        "ln": {
          "id": "xxxxxxx",
          "first_name": "xxxxxxx",
          "last_name": "xxxxxxx"
        },
        "tw": {
          "id": "xxxxxxx",
          "first_name": "xxxxxxx",
          "last_name": ""
        },
        "ig": {
          "id": "xxxxxxx",
          "first_name": "xxxxxxx",
          "last_name": "",
          "username": "xxxxxxx"
        },
        "fb": {
          "id": "xxxxxxx",
          "first_name": "xxxxxxx",
          "last_name": "xxxxxxx",
          "extra_data": {
            "timeline_album_id": "xxxxxxx"
          }
        }
      },
      "pic": "xxxxxxx.jpg"
    }
  }
  ]
}
}

  • 我的模范班是

    public class SAccountConnectModel {
     /* Social Account name*/
    public String SAccount_facebook = "false";
    public String SAccount_linkedin = "false";
    public String SAccount_twitter = "false";
    public String SAccount_instagram = "false";
    public String SAccount_youtube = "false";
    
    public String getSAccount_facebook() {
    return SAccount_facebook;
    }
    
    public void setSAccount_facebook(String SAccount_facebook) {
    this.SAccount_facebook = SAccount_facebook;
    }
    
    public String getSAccount_linkedin() {
    return SAccount_linkedin;
    }
    
    public void setSAccount_linkedin(String SAccount_linkedin) {
    this.SAccount_linkedin = SAccount_linkedin;
    }
    
    public String getSAccount_twitter() {
    return SAccount_twitter;
    }
    
    public void setSAccount_twitter(String SAccount_twitter) {
    this.SAccount_twitter = SAccount_twitter;
    }
    
    public String getSAccount_instagram() {
    return SAccount_instagram;
    }
    
    public void setSAccount_instagram(String SAccount_instagram) {
    this.SAccount_instagram = SAccount_instagram;
    }
    
    public String getSAccount_youtube() {
    return SAccount_youtube;
    }
    
    public void setSAccount_youtube(String SAccount_youtube) {
    this.SAccount_youtube = SAccount_youtube;
    }
    }
    
  • 代码是

    //Decleration
    private SAccountConnectModel mSAccountConnectModel = new      SAccountConnectModel();
    private ArrayList<SAccountConnectModel> mSAccountConnectModelArrayList = new ArrayList<SAccountConnectModel>();
    
    //Code
    /*Leader Board Network Connectivity Check and Display only connected network*/
                JSONObject mainObject = null, resultObject = null, userObject = null, userDetailObject = null, sAccountObject = null,
                        fbNetwokObject = null, twNetworkObject = null, igNetworkObject = null, lnNetworkObject = null, ytNetworkObject = null;
                try {
                    mainObject = new JSONObject(response);
                    resultObject = mainObject.getJSONObject("result");
                    if (mainObject != null) {
                        JSONArray dataJsonArray = resultObject.getJSONArray("data");
                        if (dataJsonArray != null) {
                            for (int i = 0; i < dataJsonArray.length(); i++) {
                                Logger.e(TAG, "My Response Data : " + dataJsonArray.get(i));
                                userObject = (JSONObject) dataJsonArray.get(i);
                                if (userObject != null) {
                                    userDetailObject = (JSONObject) userObject.get("user_details");
                                    Logger.e(TAG, "My Response User Details Data : " + userDetailObject);
                                    if (userDetailObject != null) {
                                        sAccountObject = userDetailObject.getJSONObject("saccounts");
                                        Logger.e(TAG, "My Response Social Account Data : " + sAccountObject);
                                        if (sAccountObject != null) {
                                            try {
                                                fbNetwokObject = sAccountObject.getJSONObject("fb");
                                                twNetworkObject = sAccountObject.getJSONObject("tw");
                                                igNetworkObject = sAccountObject.getJSONObject("ig");
                                                lnNetworkObject = sAccountObject.getJSONObject("ln");
                                                ytNetworkObject = sAccountObject.getJSONObject("yt");
                                                if (fbNetwokObject != null)
                                                    mSAccountConnectModel.setSAccount_facebook("true");
                                                else
                                                    mSAccountConnectModel.setSAccount_facebook("false");
    
                                            } catch (JSONException e) {
    
                                            }
                                            mSAccountConnectModelArrayList.add(i,mSAccountConnectModel);
                                        }
                                        /*if (sAccountObject != null) {
                                            *//*Check Facebook Connection Available or not*//*
                                            if (sAccountObject.getJSONObject("fb") != null)
                                                mSAccountConnectModel.setSAccount_facebook("true");
                                            else
                                                mSAccountConnectModel.setSAccount_facebook("false");
                                            *//*Check LinkedIn Connection Available or not*//*
                                            if (sAccountObject.getJSONObject("ln") != null)
                                                mSAccountConnectModel.setSAccount_linkedin("true");
                                            else
                                                mSAccountConnectModel.setSAccount_linkedin("false");
                                            *//*Check Instagram Connection Available or not*//*
                                            if (sAccountObject.getJSONObject("ig") != null)
                                                mSAccountConnectModel.setSAccount_instagram("true");
                                            else
                                                mSAccountConnectModel.setSAccount_instagram("false");
                                            *//*Check YouTube Connection Available or not*//*
                                            if (sAccountObject.getJSONObject("yt") != null)
                                                mSAccountConnectModel.setSAccount_youtube("true");
                                            else
                                                mSAccountConnectModel.setSAccount_youtube("false");
                                            if (sAccountObject.getJSONObject("tw") != null)
                                                mSAccountConnectModel.setSAccount_twitter("true");
                                            else
                                                mSAccountConnectModel.setSAccount_twitter("false");
                                        }*/
                                    }
                                }
    
                                Logger.e(TAG, "Model Size is : " + mSAccountConnectModelArrayList.size());
                            }
                        }
                        Logger.e(TAG, "Model Size is : " + mSAccountConnectModelArrayList.size());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
    
    //去宽容
    私有SAccountConnectModel mSAccountConnectModel=新SAccountConnectModel();
    私有ArrayList mSAccountConnectModelArrayList=新ArrayList();
    //代码
    /*引线板网络连接检查并仅显示连接的网络*/
    JSONObject mainObject=null,resultObject=null,userObject=null,userDetailObject=null,sAccountObject=null,
    FBNetworkObject=null,twNetworkObject=null,igNetworkObject=null,lnNetworkObject=null,ytNetworkObject=null;
    试一试{
    mainObject=新的JSONObject(响应);
    resultObject=mainObject.getJSONObject(“结果”);
    if(mainObject!=null){
    JSONArray dataJsonArray=resultObject.getJSONArray(“数据”);
    if(dataJsonArray!=null){
    for(int i=0;i

首先使用下面的行检查特定json对象中的键

例如:

if(sAccountObject.has("fb")){

 fbNetwokObject = sAccountObject.getJSONObject("fb");
}

如果这不是您的问题,请在此处添加崩溃报告。

为什么不将get替换为opt

范例 代替
twNetworkObject=sAccountObject.getJSONObject(“tw”)带有
twNetworkObject=sAccountObject.optJSONOb