【发布时间】:2013-12-10 04:07:30
【问题描述】:
我的代码中有位于 try/catch 中的字符串。我需要访问同一类其他部分的字符串,但我不能。我尝试删除 try/catch,但它不会让我不抛出以下错误:
未处理的异常类型 JSONException
这是我的 try/catch 代码。将其放入普通类的最佳方法是什么,以便我可以访问同一类中其他代码部分的字符串? (此代码在默认的 oncreate 包中)
try {
// Getting JSON Array
user = json.getJSONArray(TAG_USER);
JSONObject c = user.getJSONObject(0);
// Storing JSON item in a String Variable
String name = c.getString(TAG_NAME);
String messages = c.getString(TAG_MESSAGES);
String wins = c.getString(TAG_WINS);
String display = c.getString(TAG_DISPLAY);
String email = c.getString(TAG_EMAIL);
String pw = c.getString(TAG_PW);
String created = c.getString(TAG_CREATED);
String updated = c.getString(TAG_UPDATED);
//Importing TextView
TextView name1 = (TextView)findViewById(R.id.tvfullname);
TextView messages1 = (TextView)findViewById(R.id.envelope);
TextView wins1 = (TextView)findViewById(R.id.wins);
TextView created1 = (TextView)findViewById(R.id.tvcreated_at);
TextView updated1 = (TextView)findViewById(R.id.tvupdated_at);
//Set JSON Data in its respectable TextView
name1.setText("Hello " + name);
updated1.setText("Your last login was " + updated);
// print error if applicable.
} catch (JSONException e) {
e.printStackTrace();
}
【问题讨论】:
标签: java android json try-catch