Android没有得到整个数据

时间:2013-07-21 18:26:30

标签: android xml-parsing httpurlconnection

我正在尝试从服务器下载XML文件,我使用XMLpull解析器来处理它,但它并不是每次都下载整个数据。即使我试着等待下载它(线程睡眠)。你知道为什么会这样或者如何解决这个问题吗? 这是我的功能,下载文件

    /*XML read*/
private String downloadUrl(String myurl) throws IOException {
    InputStream is = null;
    int len = 100000;

    try {
        URL url_get = new URL(myurl);
        HttpURLConnection conn = (HttpURLConnection) url_get.openConnection();

        conn.setReadTimeout(15000 /* milliseconds */);
        conn.setConnectTimeout(15000 /* milliseconds */);

        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        // Starts the query
        conn.connect();
        is = conn.getInputStream();
        String contentAsString = readIt(is, len);  
        return contentAsString;

    // Makes sure that the InputStream is closed after the app is
    // finished using it.
    } finally {
        if (is != null) {
            is.close();
        } 
    }
}

public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {

    Reader reader = null;

    reader = new InputStreamReader(stream, "UTF-8");        
    char[] buffer = new char[len];
    reader.read(buffer);


    return new String(buffer);

}

我正在使用线程来启动课程功能。

以下是主题:

Thread thread =  new Thread(new Runnable() { 
        public void run() {

            try {
                Thread.sleep(100);

            while (while_start)
            {
                if(While_getadat){



                try {


                get_xml = downloadUrl(URL_IMEI);
                Thread.sleep(2000); 

                Global_XML_data=red_xml(get_xml);
                Thread.sleep(1000);
                While_getadat=false;

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                                }

            }
                }

                            }

更新

有趣的是,在调试模式下,程序正常工作,我得到了每一个数据

1 个答案:

答案 0 :(得分:0)

您是否考虑使用AsyncTask而不是手动启动线程?以下是POST(在您的问题中与GET相对)到URL的工作示例:

/**
 * Call a RESTful API to post a json
 * @param url
 * @param jsonObject
 */
private void postJson(URL url, JSONObject jsonObject) {
    HttpURLConnection conn = null;
    try {
        conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setDoOutput(true);
        conn.setChunkedStreamingMode(0);
        conn.setUseCaches(false);

        OutputStream out = new BufferedOutputStream(conn.getOutputStream());
        String jsonString = jsonObject.toString();
        Log.i("ArtistRegistrationActivity", "Sending " + jsonString + " to " + conn.getURL().toString());
        out.write(jsonString.getBytes());
        out.flush();
        out.close();
    } catch (IOException e) {
        Log.e("ArtistRegistrationActivity", e.toString());
    } finally {
        conn.disconnect();
    }
}

/**
 * Call postJson in a separate thread.  Takes a URL and a JSONObject as param
 */
private class PostJsonTask extends AsyncTask<Object, Void, Void> {
    @Override
    protected Void doInBackground(Object... urlAndJson) {
        // params[0] is the url, params[1] is the Json to sent.
        URL url = (URL) urlAndJson[0];
        JSONObject json = (JSONObject) urlAndJson[1];
        postJson(url, json);
        return null;
    }

}