我正在尝试使用SAX解析器来解析HTTP响应。除了程序没有进入我的for
循环外,一切似乎都正常工作。这是我的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playerinfo);
TableLayout t1 = (TableLayout) findViewById(R.id.myTable);
Intent i = getIntent();
Bundle b = i.getExtras();
final String str = b.getString("ARRIVING_FROM");
d=d+"";
d = tryLogin(str);
System.out.println("Value of D"+d.substring(0, 1));
this.tryLogin(str);
final ScoreList scorelist = XMLHandler.scorelist ;
id = new TextView[scorelist.getName().size()];
name = new TextView[scorelist.getName().size()];
for (int j = 0; j < scorelist.getName().size(); j++) {
TableRow tr = new TableRow(this);
id[j]= new TextView(this);
id[j].setText(scorelist.getId().get(j));
id[j].setTextColor(Color.WHITE);
id[j].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
tr.addView(id[j]);
name[j]= new TextView(this);
name[j].setText(scorelist.getName().get(j));
name[j].setTextColor(Color.WHITE);
name[j].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
tr.addView(name[j]);
t1.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
}
}
protected String tryLogin(String str) {
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://ip address/test/players_detail.php?id="+str);
List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
nvps.add(new BasicNameValuePair("id", str));
try {
UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps,HTTP.UTF_8);
httppost.setEntity(p_entity);
HttpResponse response = client.execute(httppost);
Log.v("MyPlayerInfo", response.getStatusLine().toString());
HttpEntity responseEntity = response.getEntity();
InputStream in=responseEntity.getContent();
byte[] bData = new byte[1024];
in.read(bData);
System.out.println("In Data"+in.toString());
String st=new String (bData);
d=st;
System.out.println("Response String from server"+st);
Log.v("MyPlayerInfo", "Set response to responseEntity");
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
System.out.println("Response from server"+responseEntity);
XMLHandler myXMLHandler = new XMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(retrieveInputStream(responseEntity));
System.out.println("Server Response"+responseEntity);
return d;
} catch(Exception e) {
Log.i("Catch","Exception generate in Post"+e);
e.printStackTrace();
}
return "0";
}
private InputSource retrieveInputStream(HttpEntity httpEntity) {
InputSource insrc = (InputSource) httpEntity;
try {
insrc = new InputSource(httpEntity.getContent());
} catch (Exception e) {}
return insrc;
}
logcat响应是:
04-01 12:04:21.746: ERROR/AndroidRuntime(808): FATAL EXCEPTION: main
04-01 12:04:21.746: ERROR/AndroidRuntime(808): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.example/android.example.MyPlayerInfo}: java.lang.NullPointerException
04-01 12:04:21.746: ERROR/AndroidRuntime(808): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-01 12:04:21.746: ERROR/AndroidRuntime(808): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-01 12:04:21.746: ERROR/AndroidRuntime(808): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-01 12:04:21.746: ERROR/AndroidRuntime(808): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-01 12:04:21.746: ERROR/AndroidRuntime(808): at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 12:04:21.746: ERROR/AndroidRuntime(808): at android.os.Looper.loop(Looper.java:123)
04-01 12:04:21.746: ERROR/AndroidRuntime(808): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-01 12:04:21.746: ERROR/AndroidRuntime(808): at java.lang.reflect.Method.invokeNative(Native Method)
04-01 12:04:21.746: ERROR/AndroidRuntime(808): at java.lang.reflect.Method.invoke(Method.java:521)
04-01 12:04:21.746: ERROR/AndroidRuntime(808): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-01 12:04:21.746: ERROR/AndroidRuntime(808): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-01 12:04:21.746: ERROR/AndroidRuntime(808): at dalvik.system.NativeStart.main(Native Method)
04-01 12:04:21.746: ERROR/AndroidRuntime(808): Caused by: java.lang.NullPointerException
04-01 12:04:21.746: ERROR/AndroidRuntime(808): at android.example.MyPlayerInfo.onCreate(MyPlayerInfo.java:67)
04-01 12:04:21.746: ERROR/AndroidRuntime(808): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-01 12:04:21.746: ERROR/AndroidRuntime(808): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-01 12:04:21.756: WARN/ActivityManager(59): Force finishing activity android.example/.MyPlayerInfo
04-01 12:04:21.766: WARN/ActivityManager(59): Force finishing activity android.example/.MyParsingExample
04-01 12:04:22.266: WARN/ActivityManager(59): Activity pause timeout for HistoryRecord{43ff6188 android.example/.MyPlayerInfo}
答案 0 :(得分:1)
好的,所以你在MyPlayerInfo.java的第67行遇到了NullPointerException。看看那一行,找出你获得异常的原因并修复它。
如果不知道第67行是哪一行,那么进一步提供帮助就很棘手了 - 你已经提出了很多代码要查看,我们不知道它在哪里破碎,但你应该。
(顺便说一下,你应该在finally块中关闭输入流,只捕获更具体的异常,但一次只能做一件事......)