我正在尝试使用asynctask
访问服务器上的文件,这是我的代码:
new PostTask().execute("http://antoniofalcone.it/fantavoti/gazz.csv");
@Override
protected String doInBackground(String... params) {
String url=params[0];
HttpGet httpGet = new HttpGet(url);
HttpResponse response = null;
try {
response = httpClient.execute(httpGet, localContext);
} catch (ClientProtocolException e) {
Log.d("clientprotocol", log2);
e.printStackTrace();
} catch (IOException e) {
Log.d("IOexception: ", log2);
e.printStackTrace();
}
try {
reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent()
)
);
} catch (IllegalStateException e) {
Log.d("Illegalstate: ", log2);
e.printStackTrace();
} catch (IOException e) {
Log.d("IOException2 ", log2);
e.printStackTrace();
}
return "All Done!";
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
}
我必须阅读该csv文件,但我在Nullpointerexception
上获得BufferedReader ("reader")
。在资产目录中处理本地文件没关系。
任何人都可以帮助我吗?
这是完整的logcat:
08-31 18:16:54.930: W/dalvikvm(2218): threadid=1: thread exiting with uncaught exception (group=0xa4ceeb20)
08-31 18:16:54.930: E/AndroidRuntime(2218): FATAL EXCEPTION: main
08-31 18:16:54.930: E/AndroidRuntime(2218): Process: com.falc1.fantacalcio2, PID: 2218
08-31 18:16:54.930: E/AndroidRuntime(2218): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.falc1.fantacalcio2/com.falc1.fantacalcio2.MainActivity}: java.lang.NullPointerException
08-31 18:16:54.930: E/AndroidRuntime(2218): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
08-31 18:16:54.930: E/AndroidRuntime(2218): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-31 18:16:54.930: E/AndroidRuntime(2218): at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-31 18:16:54.930: E/AndroidRuntime(2218): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-31 18:16:54.930: E/AndroidRuntime(2218): at android.os.Handler.dispatchMessage(Handler.java:102)
08-31 18:16:54.930: E/AndroidRuntime(2218): at android.os.Looper.loop(Looper.java:136)
08-31 18:16:54.930: E/AndroidRuntime(2218): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-31 18:16:54.930: E/AndroidRuntime(2218): at java.lang.reflect.Method.invokeNative(Native Method)
08-31 18:16:54.930: E/AndroidRuntime(2218): at java.lang.reflect.Method.invoke(Method.java:515)
08-31 18:16:54.930: E/AndroidRuntime(2218): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-31 18:16:54.930: E/AndroidRuntime(2218): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-31 18:16:54.930: E/AndroidRuntime(2218): at dalvik.system.NativeStart.main(Native Method)
08-31 18:16:54.930: E/AndroidRuntime(2218): Caused by: java.lang.NullPointerException
08-31 18:16:54.930: E/AndroidRuntime(2218): at fragments.VotiFragment.readCsv(VotiFragment.java:178)
08-31 18:16:54.930: E/AndroidRuntime(2218): at fragments.VotiFragment.onCreateView(VotiFragment.java:75)
08-31 18:16:54.930: E/AndroidRuntime(2218): at android.app.Fragment.performCreateView(Fragment.java:1700)
08-31 18:16:54.930: E/AndroidRuntime(2218): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
08-31 18:16:54.930: E/AndroidRuntime(2218): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
08-31 18:16:54.930: E/AndroidRuntime(2218): at android.app.BackStackRecord.run(BackStackRecord.java:684)
08-31 18:16:54.930: E/AndroidRuntime(2218): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
08-31 18:16:54.930: E/AndroidRuntime(2218): at android.app.Activity.performStart(Activity.java:5240)
08-31 18:16:54.930: E/AndroidRuntime(2218): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
08-31 18:16:54.930: E/AndroidRuntime(2218): ... 11 more
答案 0 :(得分:2)
语句response = httpClient.execute(httpGet, localContext);
被try / catch块正确包装,但如果抛出异常(例如服务器无法访问,URL不正确或网络中断的情况),则catch块只打印异常,继续。这意味着随后的陈述:
reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent()
)
将触发空指针异常,因为response
为空。打印出错误异常后,httpClient.execute语句的catch块应该只有return
。更好的方法是为请求和响应解析提供一组try catch块。如下所示:
HttpResponse response = null;
try {
HttpGet httpGet = new HttpGet(url);
response = httpClient.execute(httpGet, localContext);
reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
} catch (IOException ioex) {
Log.d(TAG, "IOException", ioex);
} catch (IllegalStateException ise) {
Log.d(TAG, "IllegalStateException", ise);
}
由于日志语句可以使用消息打印异常,因此我只是为您组合。您不需要reduntant printStateTrace语句。由于ClientProtocolException继承自IOException,因此您只需要捕获基类。
对于奖金,您可以通过在类中声明“TAG”变量来使您的日志语句更具可过滤性,如下所示:
public static final String TAG = VotiFragment.class.getSimpleName();
现在我已经完成了清理代码,我刚刚意识到你遇到了另一个问题。你的asynctask的重点是避免在主线程上进行网络I / O.但是你的所有asynctask都是启动HTTP请求,但实际上并不解析响应。它只是创建了一个读者对象,我猜你实际上是从你的UI线程流出来的。这意味着主线程上的剩余网络i / o除非响应被完全缓冲。我建议您在后台任务中进行所有解析。
答案 1 :(得分:0)
这是readCsv
代码:
public void readCsv()抛出IOException {
String line = "";
db.beginTransaction();
try {
while ((line = reader.readLine()) != null) {
String[] colums = line.split(",");
if (colums.length > 23) {
Log.d("CSVParser", "Skipping Bad CSV Row");
continue;
}
Player play1 = new Player(colums[1].trim(), colums[2].trim(), Integer.parseInt(colums[3]),
Integer.parseInt(colums[4]), Integer.parseInt(colums[5]),
Double.parseDouble(colums[6]), Double.parseDouble(colums[7]), Integer.parseInt(colums[8]),
Double.parseDouble(colums[9]), Integer.parseInt(colums[10]),
Integer.parseInt(colums[11]), Integer.parseInt(colums[12]));
db.addPlayer(play1);
}
} catch (IOException e) {
e.printStackTrace();
}
db.setTransactionSuccessful();
db.beginTransaction();
}