Question

I'm trying to connect and retrieve the data from xml file from server. The data usage in get the xml data is quite large for a single data in the xml file. It exceeded up to 700 bytes where if i'm open directly from the browser in android phone, it is only 18 bytes.

Why there is so much different on number of bytes when getting data through app? Below is my asynctask code on getting xml data from server. Thank You

public class GetXMLTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
    if (!isCancelled()) {
        try {
            String output = null;
            //String[] newurl = { GlobalVariables.Global_URL + "/appstatus.xml" };
            for (String url : urls) {
                output = getOutputFromUrl(url);
            }

            return output;
        } catch (Exception e) {
            return null;
        }
    }
    else
    {
        return null;
    }
}
@Override
  protected void onCancelled() {

  }

private String getOutputFromUrl(String url) {
    StringBuffer output = new StringBuffer("");
    try {
        InputStream stream = getHttpConnection(url);
        BufferedReader buffer = new BufferedReader(new InputStreamReader(
                stream));
        String s = "";
        while ((s = buffer.readLine()) != null)
            output.append(s);
    } catch (IOException e1) {
        e1.printStackTrace();

    }
    return output.toString();
}

// Makes HttpURLConnection and returns InputStream
private InputStream getHttpConnection(String urlString) throws IOException {
    InputStream stream = null;
    URL url = new URL(urlString);
    URLConnection connection = url.openConnection();

    try {
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
        httpConnection.setRequestMethod("GET");
        httpConnection.connect();

        if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            stream = httpConnection.getInputStream();
        }
    } catch (Exception ex) {
        ex.printStackTrace();

    }
    return stream;
}

@Override
protected void onPostExecute(String output) {
    String XML = output;
    // GlobalVariables.Global_data = output;
    // String XML = prepareXML();

    try {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        /** Create handler to handle XML Tags ( extends DefaultHandler ) */
        MyXMLHandler myXMLHandler = new MyXMLHandler();
        xr.setContentHandler(myXMLHandler);

        ByteArrayInputStream is = new ByteArrayInputStream(XML.getBytes());
        xr.parse(new InputSource(is));
    } catch (Exception e) {

    }
}

}

Was it helpful?

Solution

How are you measuring the data usage? I think you are confused. You might be looking at the file size of 18 bytes when taking via browser, but calculating the request + response (including file) while though mobile device.

If you concerned about the packet size of HTTP, upgrade to WebSockets

Intro to WebSockets and Why

WebSockets in Android

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top