开发者

HTTP POST + ANDROID Not Getting response !

开发者 https://www.devze.com 2023-02-06 03:21 出处:网络
I am having webservice made with PHP. I am posting parameter and fetching data from the server. It works fine for the small amount of data.

I am having webservice made with PHP. 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. The same webservice with all the 160 xml records is not fetching data.

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 ht开发者_如何学JAVAtpclient = 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.


This is not an answer, but a recommendation - use BasicResponseHandler for retrieving HTTP content. It saves all the streaming/buffering nonsense! (i.e. most of your try clause)

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消