Reputation: 19
I have a problem : I use a web service to return some values , but some times some problems face the web services , and when I want to display them my app crashes , so I want to make sure that if there is nothing to show then return just empty space , and I use it on JSON , there is a part of my code if someone can help me ,
public static Article parseArticle(JSONObject jsonArticle) {
Article article = new Article();
try {
article.setTitle(ArabicUtilities.reshape(Html.fromHtml(jsonArticle.getString("title")).toString()));
article.setExcerpt(ArabicUtilities.reshape(Html.fromHtml(jsonArticle.getString("excerpt")).toString()));
article.setContent(ArabicUtilities.reshape(Html.fromHtml(jsonArticle.getString("content")).toString()));
article.setDate(jsonArticle.getString("date"));
return article;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
private void loadinfo() {
{
programs = JSONParser.parseProgram(savedData);
txt1.setText(article.get(1).getTitle());
txt2.setText(article.get(0).getTitle());
txt3.setText(article.get(1).excerpt());
txt4.setText(article.get(0).excerpt());
txt5.setText(article.get(1).content());
txt6.setText(article.get(0).content());
txt7.setText(article.get(1).date());
txt8.setText(article.get(0).date());
}
this is what happened when i try to have information it give error on
txt1.setText(article.get(1).getTitle());
that there is no values
Upvotes: 0
Views: 199
Reputation: 157437
If I have not misunderstood you instead of jsonArticle.getString
you can use jsonArticle.optString
. It will return an empty String
if the json does not contain that key.
String mString = jsonArticle.optString("title");
article.setTitle(ArabicUtilities.reshape(Html.fromHtml(mString).toString()));
mString = jsonArticle.optString("excerpt");
article.setExcerpt(ArabicUtilities.reshape(Html.fromHtml(mString).toString()));
mString = jsonArticle.optString("content");
article.setContent(ArabicUtilities.reshape(Html.fromHtml(mString).toString()));
mString = jsonArticle.optString("date");
article.setDate(mString);
here the doc for optString()
EDIT
public static Article parseArticle(JSONObject jsonArticle) {
Article article = new Article();
try {
String mString = jsonArticle.optString("title");
article.setTitle(ArabicUtilities.reshape(Html.fromHtml(mString).toString()));
mString = jsonArticle.optString("excerpt");
article.setExcerpt(ArabicUtilities.reshape(Html.fromHtml(mString).toString()));
mString = jsonArticle.optString("content");
article.setContent(ArabicUtilities.reshape(Html.fromHtml(mString).toString()));
mString = jsonArticle.optString("date");
article.setDate(mString);
} catch (JSONException e) {
e.printStackTrace();
}
return article;
}
Upvotes: 1
Reputation: 715
public static Article parseArticle(JSONObject jsonArticle) {
Article article = new Article();
if(null!=jsonArticle){
try {
/*your code */
}else{
article.setTitle(ArabicUtilities.reshape("");
article.setExcerpt(ArabicUtilities.reshape("");
article.setContent(ArabicUtilities.reshape("");
article.setDate("");
return article;
}
}
Upvotes: 0