我通过使用 HTTP-post 发送值,但发生异常我的代码如下:-
public static InputStream SendDetails(String senderEmail,String senderName,String senderPhone,String comment )
{
if(item != null)
{
url = constants.STATEURL + item.getListingid().toString()+"/contact";//?access_token=0|V1CALGARY|e98984a4c954fa1d78e3be626ad81a1d&mode=map";
}
else if(itemsavedfromdb != null)
{
url = constants.STATEURL + itemsavedfromdb.getListingid().toString()+"/contact";//?access_token=0|V1CALGARY|e98984a4c954fa1d78e3be626ad81a1d&mode=map";
}
else if(itemrecentfromdb != null)
{
url = constants.STATEURL + itemrecentfromdb.getListingid().toString()+"/contact";//?access_token=0|V1CALGARY|e98984a4c954fa1d78e3be626ad81a1d&mode=map";
}
List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>();
nameValuePairs.add(new BasicNameValuePair("access_token",constants.accesstokenstate));
nameValuePairs.add(new BasicNameValuePair("mode","map"));
nameValuePairs.add(new BasicNameValuePair("senderEmail",senderEmail));
nameValuePairs.add(new BasicNameValuePair("senderName",senderName));
nameValuePairs.add(new BasicNameValuePair("senderPhone",senderPhone));
nameValuePairs.add(new BasicNameValuePair("comment",comment));
HttpPost request = new HttpPost(url);
request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(request);
return response.getEntity().getContent();
这个方法被称为:-
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
mailHandler myhandler = new mailHandler();
xr.setContentHandler(myhandler);
String mailID = youremail.getText().toString();
if(mailID != null && !mailID.equalsIgnoreCase("") )
{
System.out.println("1");
ios = SendDetails(mailID,yourName.getText().toString(),yourPhoneNo.getText().toString(),yourMessage.getText().toString());
System.out.println("ios-----"+ios);
String strResponse = convertStreamToString(ios);
System.out.println("response"+strResponse);
xr.parse(new InputSource(ios));
resultmail = myhandler.emailresultdata();
customizeDialog = new CustomizeDialog(PropertyDetail.this);
customizeDialog.setTitle("Success");
customizeDialog.setMessage("Email send successfully");
customizeDialog.show();
由于数据未成功发送,上述代码发生异常
以下是堆栈跟踪:-
07-03 09:02:15.264: WARN/System.err(491): java.io.IOException: Attempted read on closed stream.
07-03 09:02:15.275: WARN/System.err(491): at org.apache.http.conn.EofSensorInputStream.isReadAllowed(EofSensorInputStream.java:127)
07-03 09:02:15.275: WARN/System.err(491): at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:176)
07-03 09:02:15.275: WARN/System.err(491): at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:516)
07-03 09:02:15.275: WARN/System.err(491): at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:479)
07-03 09:02:15.275: WARN/System.err(491): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:318)
07-03 09:02:15.275: WARN/System.err(491): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:275)
07-03 09:02:15.275: WARN/System.err(491): at com.rentfaster.home.PropertyDetail.onClick(PropertyDetail.java:1623)
07-03 09:02:15.275: WARN/System.err(491): at java.lang.reflect.Method.invokeNative(Native Method)
07-03 09:02:15.275: WARN/System.err(491): at java.lang.reflect.Method.invoke(Method.java:507)
07-03 09:02:15.275: WARN/System.err(491): at android.view.View$1.onClick(View.java:2139)
07-03 09:02:15.284: WARN/System.err(491): at android.view.View.performClick(View.java:2485)
07-03 09:02:15.284: WARN/System.err(491): at android.view.View$PerformClick.run(View.java:9080)
07-03 09:02:15.284: WARN/System.err(491): at android.os.Handler.handleCallback(Handler.java:587)
07-03 09:02:15.284: WARN/System.err(491): at android.os.Handler.dispatchMessage(Handler.java:92)
07-03 09:02:15.284: WARN/System.err(491): at android.os.Looper.loop(Looper.java:130)
07-03 09:02:15.284: WARN/System.err(491): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-03 09:02:15.284: WARN/System.err(491): at java.lang.reflect.Method.invokeNative(Native Method)
07-03 09:02:15.284: WARN/System.err(491): at java.lang.reflect.Method.invoke(Method.java:507)
07-03 09:02:15.284: WARN/System.err(491): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-03 09:02:15.294: WARN/System.err(491): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-03 09:02:15.294: WARN/System.err(491): at dalvik.system.NativeStart.main(Native Method)
我的 convertStreamToString 方法是:-
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return sb.toString();
}
任何人都建议我我错在哪里以及正确的方法是什么。