简体   繁体   English

HTTP POST + ANDROID无法获得响应!

[英]HTTP POST + ANDROID Not Getting response !

I am having webservice made with PHP. 我正在使用PHP进行Web服务。 I am posting parameter and fetching data from the server. 我正在发布参数并从服务器获取数据。 It works fine for the small amount of data. 它适用于少量数据。

I have one test service which return 10 xml records its working well. 我有一个测试服务,该服务返回10个xml记录,并且运行良好。 The same webservice with all the 160 xml records is not fetching data. 具有所有160条xml记录的相同Web服务未获取数据。

I pasted my code below.. Is there any limitation in terms of amount of data or do I need to pass any other parameters ? 我在下面粘贴了我的代码。. 在数据量方面是否有任何限制,或者是否需要传递任何其他参数?

public void LoadNews(String Url, List<NameValuePair> nvp) {
    InputStream ins = null;
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(Url);
    httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = nvp;
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

        ins = response.getEntity().getContent();
        BufferedInputStream bis = new BufferedInputStream(ins);
        ByteArrayBuffer baf = new ByteArrayBuffer(1024);

        int current = 0;
        while ((current = bis.read()) != -1) {
            baf.append((byte) current);
        }
        String response = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
                + new String(baf.toByteArray());
    } catch (ClientProtocolException e) {
        Log.e(CommonFunctions.LOGTAG, "ClientProtocolException");
    } catch (IOException e) {
        Log.e(CommonFunctions.LOGTAG, "IOException");
    }

I tested same webservice with Firefox + Poster Addon its working fine with.. XML gives CDATA in response. 我使用Firefox + Poster Addon测试了相同的Web服务,并与之配合使用。

This is not an answer, but a recommendation - use BasicResponseHandler for retrieving HTTP content. 这不是答案,而是建议-使用BasicResponseHandler检索HTTP内容。 It saves all the streaming/buffering nonsense! 它节省了所有的流/缓冲废话! (ie most of your try clause) (即您的大多数try子句)

String response = httpclient.execute(httppost, new BasicResponseHandler());

To make this clearer: 为了使这一点更清楚:

public void LoadNews(String Url, List<NameValuePair> nvp) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(Url);
    httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = nvp;
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        String response = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 
            httpclient.execute(httppost, new BasicResponseHandler());

        /* ... do stuff with response ... */
    } catch (ClientProtocolException e) {
        Log.e(CommonFunctions.LOGTAG, "ClientProtocolException");
    } catch (IOException e) {
        Log.e(CommonFunctions.LOGTAG, "IOException");
    }
}

As an addendum, you should not be adding the XML header manually - this should be returned in the document by the web service you're using. 作为附录,您应该手动添加XML标头-您所使用的Web服务应在文档中返回该标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM