我有一個奇怪的問題。我有這個android應用程序應該從一個導致文本文件的URL中獲取一些數據。它有18行,每行一個項目,看起來是這樣的:掛在按鈕上按
萬事達
名
(這是一個任務,所有的數據顯然是假的)
我有這樣的:
public class CreditCard {
//set up method to get and return data as arraylist
public ArrayList<String> ccArray(){
ArrayList<String> ccInfo = new ArrayList<String>();
//array set up. time to get data
try {
URL urlDat = new URL("url");
Scanner urlScn = new Scanner(urlDat.openStream());
while (urlScn.hasNext()){
ccArray().add(urlScn.nextLine() + "\n");
}
}catch (MalformedURLException URLe){
//handle exception
}
catch (IOException IOe){
//handle exception
}
return ccInfo;
}
}
這樣我就可以通過陣列的主要方法(或類的另一種方法,不知道然而)。我有一個按鈕,運行中的主要方法displayCreditCards:
public void displayCreditCards(View view) throws IOException {
TextView tv1;
tv1 = (TextView) findViewById(R.id.text_main);
tv1.setText("Data will appear here");
//create objects to det data and make profiles
CreditCard ccObj = new CreditCard();
tv1.setText(ccObj.ccArray().toString());
}
然而,這會導致應用程序沒有響應,並從我的Android得等待/關閉對話框。我不知道如何解釋logcat:
10-04 18:27:01.204 9036-9036/? I/art﹕ Not late-enabling -Xcheck:jni (already on)
10-04 18:27:01.204 9036-9036/? I/art﹕ Late-enabling JIT
10-04 18:27:01.206 9036-9036/? I/art﹕ JIT created with code_cache_capacity=2MB compile_threshold=1000
10-04 18:27:01.257 9036-9036/? W/System﹕ ClassLoader referenced unknown path: /data/app/com.example.pat.displaycc-2/lib/x86
10-04 18:27:01.396 9036-9058/com.example.pat.displaycc D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
10-04 18:27:01.400 9036-9036/com.example.pat.displaycc D/﹕ HostConnection::get() New Host Connection established 0xad7ad9a0, tid 9036
10-04 18:27:01.468 9036-9058/com.example.pat.displaycc D/﹕ HostConnection::get() New Host Connection established 0xad7ade40, tid 9058
10-04 18:27:01.471 9036-9058/com.example.pat.displaycc I/OpenGLRenderer﹕ Initialized EGL, version 1.4
10-04 18:27:01.520 9036-9058/com.example.pat.displaycc W/EGL_emulation﹕ eglSurfaceAttrib not implemented
10-04 18:27:01.521 9036-9058/com.example.pat.displaycc W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xad7917c0, error=EGL_SUCCESS
10-04 18:27:04.841 9036-9036/com.example.pat.displaycc W/art﹕ Before Android 4.1, method int android.support.v7.internal.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
10-04 18:27:04.874 9036-9058/com.example.pat.displaycc W/EGL_emulation﹕ eglSurfaceAttrib not implemented
10-04 18:27:04.874 9036-9058/com.example.pat.displaycc W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xad791c20, error=EGL_SUCCESS
10-04 18:27:06.138 9036-9058/com.example.pat.displaycc E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xab80f0d0
10-04 18:27:11.564 9036-9058/com.example.pat.displaycc W/EGL_emulation﹕ eglSurfaceAttrib not implemented
10-04 18:27:11.564 9036-9058/com.example.pat.displaycc W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xad791c00, error=EGL_SUCCESS
10-04 18:27:13.108 9036-9058/com.example.pat.displaycc E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xab80f060
我不知道爲什麼它掛在我身上,有什麼想法?
網絡調用只能在Main(UI)以外的線程中完成。從線程調用這個'ccArray()'(像Handle,AsyncTask,Thread)。 –