我正在为一个库创建一个应用程序,我需要访问一个远程数据库,我遇到了这个代码的问题:
llave = accountm.getUserData (mAccounts [i], accountm.KEY_USERDATA);
我在循环中增加变量i的这一行,当我的程序到达此行时出现错误,我的应用程序意外退出。在日食的logcat中,我的uid是不同的。
以下是整个功能的代码:
public ArrayList<Grabar> getItems(Context ctx, URL url)throws IOException {
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
xr.setContentHandler(this);
URLConnection conn = url.openConnection();
InputSource is;
// Build the baseURL in-case we're given relative links in our RSS feed
baseURL = url.getProtocol() + "://" + url.getHost();
if ( url.getPort() > 0 )
baseURL += ":" + url.getPort();
AccountManager accountm = AccountManager.get(ctx);
//Account[] mAccounts = accountm.getAccountsByType("nz.net.catalyst.KiritakiKoha.account");
Account[] mAccounts = accountm.getAccounts();
String llave;
for (int i=0; i < mAccounts.length; i++) {
llave=accountm.getUserData(mAccounts[i], accountm.KEY_USERDATA);
System.out.println(mAccounts[i]);
if ( llave.length() > 0 ) {
conn.setRequestProperty("Cookie", llave);
break;
}
}
is = new InputSource(conn.getInputStream());
xr.parse(is);
return Grabars;
} catch (IOException e) {
Log.e(TAG, "getItems: IOException: " + e.toString());
throw new IOException("Connection failed.");
} catch (SAXException e) {
Log.e(TAG, "getItems: SAXException: " + e.toString());
} catch (ParserConfigurationException e) {
Log.e(TAG, "getItems: ParserConfigurationException: " + e.toString());
}
return Grabars;
}