0

我通过使用 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();
}

任何人都建议我我错在哪里以及正确的方法是什么。

4

3 回答 3

1

好的,将您的代码从

xr.parse(new InputSource(ios));

InputStream input = new ByteArrayInputStream(strResponse.getBytes());
xr.parse(new InputSource(input));
于 2012-07-03T03:47:40.663 回答
0

阅读我关于在单独线程上执行网络连接的博客文章。将请求包装在 anAsyncTask或 a中Thread通常可以解决问题。它还使您的应用程序响应速度更快,更不容易出现 ANR 强制关闭对话框。

为什么冰淇淋三明治会使您的应用程序崩溃

于 2012-07-03T04:01:09.033 回答
0

您正在将输入流传递给 convertStreamToString 方法,并且您正在关闭该方法中输入流的相同实例。在该方法之后,您仍然使用相同的流实例。这就是你得到这个例外的原因。

在您的 convertStreamToString 方法中创建一个输入流的局部变量,如下所示。

private static String convertStreamToString(InputStream is) {

    InputStream inputStream = is;
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

尝试这个。

于 2012-07-03T03:57:37.593 回答