我尝试从Facebook sdk获取数据:
public void onCompleted(JSONObject object, GraphResponse response) {
String name = null;
try {
name = object.getString("name");
} catch (JSONException e) {
e.printStackTrace();
}
String profile = null;
try {
profile = object.getJSONObject("picture").getJSONObject("data").getString("url");
} catch (JSONException e) {
e.printStackTrace();
}
String cover = null;
try {
cover = object.getJSONObject("cover").getString("source");
} catch (JSONException e) {
e.printStackTrace();
}
String email = null;
try {
email = object.getString("email");
} catch (JSONException e) {
e.printStackTrace();
}
String gender = null;
try {
gender = object.getString("gender");
} catch (JSONException e) {
e.printStackTrace();
}
String birthday = null;
try {
birthday = object.getString("birthday");
} catch (JSONException e) {
e.printStackTrace();
}
new SessionManager(activity).storeUserInfo(name, profile, cover, email, gender, birthday);
}
这是非常不利的,因为有很多字段,每个字段可以为null,每个字段都需要一个try-cach博客。
我需要一个解决方案来更清晰地呈现代码。
答案 0 :(得分:4)
如果您只想删除try catch块,请使用“opt”替换“get”
(例如。optInt(Key)
,optString(Key)
..)
如果密钥不存在,请注意“opt”将返回null,这可能会导致许多空指针异常而不进行检查。
答案 1 :(得分:3)
好的,您可以通过调用object.has("key")
来检查密钥是否存在,如下所示:
public void onCompleted(JSONObject object, GraphResponse response) {
String name = "", profile = "", cover = "", email = "", gender = "", birthday = "";
try {
if (object.has("name")){
name = object.getString("name");
}
if (object.has("picture")){
profile = object.getJSONObject("picture").getJSONObject("data").getString("url");
}
if (object.has("cover")){
cover = object.getJSONObject("cover").getString("source");
}
if (object.has("email")){
email = object.getString("email");
}
if (object.has("gender")){
gender = object.getString("gender");
}
if (object.has("birthday")){
birthday = object.getString("birthday");
}
} catch (JSONException e) {
e.printStackTrace();
}
new SessionManager(activity).storeUserInfo(name, profile, cover, email, gender, birthday);
}
答案 2 :(得分:1)
我宁愿这样做:
String value = object.has(key) && !object.isNull(key) ? object.getString(key) : null;
一条线,更清洁,更容易〜:)
答案 3 :(得分:1)
您的所有字段都是强制性的吗?
你可以这样做:
try{
name = object.getString("name");
... // more methods here
new SessionManager(activity).storeUserInfo(name, profile, cover, email, gender, birthday);
}catch(JSONException e){
e.printStackTrace( );
}
这样,只有在检索完所有字段后才会到达SessionManager
。即使缺少单个字段,您也可以直接进入catch
块。
其他:
public String getString(JSONObject obj,String key,String defVal){
if( obj.has(key) )
return obj.getString( key );
return defVal;
}
现在你可以这样做:
name = getString(json,"name","UNDEFINED");
答案 4 :(得分:1)
在一个try块中添加所有语法如果任何一个语法有异常而不是go in catch ....
public void onCompleted(JSONObject object, GraphResponse response) { String name = null; try { name = object.getString("name"); String profile = null; profile = object.getJSONObject("picture").getJSONObject("data").getString("url"); String cover = null; cover = object.getJSONObject("cover").getString("source"); String email = null; email = object.getString("email"); String gender = null; gender = object.getString("gender"); String birthday = null; birthday = object.getString("birthday"); } catch (JSONException e) { e.printStackTrace(); } new SessionManager(activity).storeUserInfo(name, profile, cover, email, gender, birthday); }