IN MainActivity.java
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://192.168.5.10/ijoomer_development/index.php?option=com_ijoomer&plg_name=jomsocial&pview=user&ptask=login&tmpl=component");
StringBuffer strBuffer = new StringBuffer("<data><username>" + username + "</username><password>" + password + "</password><lat>" + lat +"</lat><long>"+ longi +"</long></data>");
StringEntity strEntity = new StringEntity(strBuffer.toString());
post.setEntity(strEntity);
HttpResponse response = client.execute(post);
InputStream in = response.getEntity().getContent();
MainActivity mActivity = new MainActivity();
System.out.println(mActivity.convertStreamToString(in));
demoHandler = new DemoHandler();
ArrayList<UserBean> users = demoHandler.parse(in);
Log.i("VALUE OF USER",""+users);
IN DemoHandler.java
public ArrayList<UserBean> parse(InputStream in) {
try {
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
parser.parse(in, this);
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return records;
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
{
data.replace(0, data.length(), "");
if(localName.equalsIgnoreCase(ROOT_TAG))
bean = new UserBean();
}
public void characters(char[] ch, int start, int length) throws SAXException {
data.append(ch, start, length);
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException
{
if(localName.equalsIgnoreCase(TAG_USERNAME))
bean.username = data.toString().trim();
else if(localName.equalsIgnoreCase(TAG_PASSWORD))
bean.password = data.toString().trim();
else if(localName.equalsIgnoreCase(TAG_LAT))
bean.lat = data.toString().trim();
else if(localName.equalsIgnoreCase(TAG_LONG))
records.add(bean);
}
public String convertStreamToString(InputStream in) throws IOException {
if (in != null) {
writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
in.close();
}
return writer.toString();
} else {
return "";
}
}
}
我在MainActivity.java
中的第ArrayList<UserBean> users = demoHandler.parse(in);
行收到错误
我在DemoHandler.java
中的第parser.parse(in, this);
行收到了另一个错误
例外:
03-19 11:25:03.108: WARN/ExpatReader(297): DTD handlers aren't supported.
03-19 11:25:03.108: WARN/System.err(297): java.io.IOException: Attempted read on closed stream.
03-19 11:25:03.169: WARN/System.err(297): at org.apache.http.conn.EofSensorInputStream.isReadAllowed(EofSensorInputStream.java:127)
03-19 11:25:03.169: WARN/System.err(297): at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:176)
03-19 11:25:03.169: WARN/System.err(297): at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:504)
03-19 11:25:03.169: WARN/System.err(297): at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:467)
03-19 11:25:03.169: WARN/System.err(297): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:329)
03-19 11:25:03.169: WARN/System.err(297): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:286)
03-19 11:25:03.209: WARN/System.err(297): at javax.xml.parsers.SAXParser.parse(SAXParser.java:361)
03-19 11:25:03.219: WARN/System.err(297): at javax.xml.parsers.SAXParser.parse(SAXParser.java:240)
03-19 11:25:03.219: WARN/System.err(297): at com.demo.parsing.DemoHandler.parse(DemoHandler.java:69)
03-19 11:25:03.219: WARN/System.err(297): at com.demo.parsing.MainActivity$1.onClick(MainActivity.java:119)
03-19 11:25:03.219: WARN/System.err(297): at android.view.View.performClick(View.java:2364)
03-19 11:25:03.219: WARN/System.err(297): at android.view.View.onTouchEvent(View.java:4179)
03-19 11:25:03.219: WARN/System.err(297): at android.widget.TextView.onTouchEvent(TextView.java:6540)
03-19 11:25:03.219: WARN/System.err(297): at android.view.View.dispatchTouchEvent(View.java:3709)
03-19 11:25:03.219: WARN/System.err(297): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-19 11:25:03.219: WARN/System.err(297): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-19 11:25:03.219: WARN/System.err(297): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-19 11:25:03.219: WARN/System.err(297): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-19 11:25:03.219: WARN/System.err(297): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
03-19 11:25:03.219: WARN/System.err(297): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
03-19 11:25:03.219: WARN/System.err(297): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
03-19 11:25:03.219: WARN/System.err(297): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
03-19 11:25:03.239: WARN/System.err(297): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
03-19 11:25:03.239: WARN/System.err(297): at android.os.Handler.dispatchMessage(Handler.java:99)
03-19 11:25:03.239: WARN/System.err(297): at android.os.Looper.loop(Looper.java:123)
03-19 11:25:03.239: WARN/System.err(297): at android.app.ActivityThread.main(ActivityThread.java:4363)
03-19 11:25:03.239: WARN/System.err(297): at java.lang.reflect.Method.invokeNative(Native Method)
03-19 11:25:03.239: WARN/System.err(297): at java.lang.reflect.Method.invoke(Method.java:521)
03-19 11:25:03.239: WARN/System.err(297): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-19 11:25:03.239: WARN/System.err(297): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-19 11:25:03.329: WARN/System.err(297): at dalvik.system.NativeStart.main(Native Method)
03-19 11:25:03.329: INFO/VALUE OF USER(297): []
答案 0 :(得分:5)
好吧,我看到你先说这个:
System.out.println(mActivity.convertStreamToString(in));
假设你的DemoHandler中的converSTreamToSTring与下面相同,这个函数调用
in.close();
所以你现在关闭了流。正如错误所说,您正在尝试在此行中对流进行处理:
ArrayList<UserBean> users = demoHandler.parse(in);
所以我对你得到java.io.IOException: Attempted read on closed stream.
错误并不感到惊讶。因为你先关闭了流,然后尝试用它做一些事情。
答案 1 :(得分:0)
我最近遇到了类似的问题,我通过删除函数末尾的method.releaseConnection()
来修复它。可能会帮助别人!