简体   繁体   English

从Web服务检索数据时遇到问题

[英]Trouble with retriving data from a web service

I want to get data from an web service and after that to display it in a listView. 我想从Web服务获取数据,然后再将其显示在listView中。 So I made a function that get the data from the service, but when I tested it I discovered something unexpectedly. 因此,我做了一个从服务中获取数据的函数,但是当我对其进行测试时,我意外地发现了一些东西。 When I tested it as a call in the main function of the java class, it works, it returns me the data, but when I use it in the listView class, it doesn't. 当我在java类的main函数中将它作为调用进行测试时,它可以工作,它会向我返回数据,但是当我在listView类中使用它时,它就不会。 After some debugging, I still don't get why it doesn't work, but I observed that the only difference is that when the function is called in the main function, the URLConnection begins with sun.net.www.protocol.http.Http.URLConnection:http://... and when it's called in the listView class it begins with com.android.okhttp.internal.huc.HttpURLConnectionImpl:http//.. . 一些调试后,我仍然不知道为什么它不工作,但我认为,唯一的区别是,当函数被调用的main功能, URLConnection开始sun.net.www.protocol.http.Http.URLConnection:http://...并且在listView类中调用它时,它以com.android.okhttp.internal.huc.HttpURLConnectionImpl:http//..开头。

public static String getDataFromServer(String url) {

    BufferedReader inputStream = null;
    URL dataUrl = null;
    String data = null;

    //handle url exception
    try {
        dataUrl = new URL(url);
        try {
            URLConnection dc = dataUrl.openConnection();
            dc.setConnectTimeout(5000);
            dc.setReadTimeout(5000);
            try {
                inputStream = new BufferedReader(new InputStreamReader(dc.getInputStream(), "UTF-8"));
            } catch (UnsupportedEncodingException e) { System.out.println(e.getMessage());}
            StringBuffer sb = new StringBuffer();
            String line = "";
            while ((line = inputStream.readLine())!=null)
                sb.append(line + "\r\n");
            data = sb.toString();
        } catch (IOException e) { System.out.println(e.getMessage());
        }
    } catch (MalformedURLException e) { System.out.println(e.getMessage());}
    return data;
}

do somthing like that : 做这样的事情:

        String url = "http://youaddres.com/path";
    URL object = new URL(url);
    HttpURLConnection con = (HttpURLConnection) object.openConnection();
    con.setDoOutput(true);
    con.setDoInput(true);
    con.setRequestProperty("Content-Type", "application/json;   charset=UTF-8");
    //if it is post
    con.setRequestMethod("POST");
    String me = "{\"json\":\"" + json+ "\",\"json\":\"" + json+"\"}";
    OutputStream os = con.getOutputStream();
    os.write(me.getBytes());
    os.flush();
    InputStream inputStr =  con.getInputStream();
    reader = new BufferedReader(new InputStreamReader(inputStr));
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line);
    }
    String  =  response = sb.toString();

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

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