Android Read contents of a URL (content missing after in result)

巧了我就是萌 提交于 2020-01-09 10:24:00

问题


I have the following code that reads the content of a url

public static String DownloadText(String url){
    StringBuffer result = new StringBuffer();
    try{
        URL jsonUrl = new URL(url);

        InputStreamReader isr  = new InputStreamReader(jsonUrl.openStream());

        BufferedReader in = new BufferedReader(isr);

        String inputLine;

        while ((inputLine = in.readLine()) != null){
            result.append(inputLine);
        }
    }catch(Exception ex){
        result = new StringBuffer("TIMEOUT");
        Log.e(Util.AppName, ex.toString());
    }
        in.close();
        isr.close();
    return result.toString();
}

The problem is I am missing content after 4065 characters in the result returned. Can someone help me solve this problem.

Note: The url I am trying to read contains a json response so everything is in one line I think thats why I am having some content missing.


回答1:


Try this:

try {
  feedUrl = new URL(url).openConnection();
} catch (MalformedURLException e) {
  Log.v("ERROR","MALFORMED URL EXCEPTION");
} catch (IOException e) {
  e.printStackTrace();
}
try {
  in = feedUrl.getInputStream();
  json = convertStreamToString(in);
}catch(Exception e){}

while convertStreamToString is:

private static String convertStreamToString(InputStream is) throws UnsupportedEncodingException {
  BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
  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 (IOException e) {
    e.printStackTrace();
  }
}
return sb.toString();
 }



回答2:


Here is a cleaner version to fetch the output of a script from the web:

public String getOnline(String urlString) {
    URLConnection feedUrl;
    try {
        feedUrl = new URL(urlString).openConnection();
        InputStream is = feedUrl.getInputStream();

        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
        StringBuilder sb = new StringBuilder();
        String line = null;

        while ((line = reader.readLine()) != null) {
            sb.append(line + "");
        }
        is.close();

        return sb.toString();

    }catch(Exception e){
        e.printStackTrace();
    }

    return null;
}

And remember that you cannot download anything from the main thread. It has to be from a separate thread. Use something like:

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

                if(!isNetworkAvailable()){
                    Toast.makeText(getApplicationContext(), getResources().getString(R.string.nointernet), Toast.LENGTH_LONG).show();
                    return;
                }

                String str=getOnline("http://www.example.com/script.php");

            }
        }).start();


来源:https://stackoverflow.com/questions/2094529/android-read-contents-of-a-url-content-missing-after-in-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!