0

I want to get data from an web service and after that to display it in a 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. 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//.. .

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;
}
Papanash
  • 59
  • 7

1 Answers1

1

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();
White Druid
  • 295
  • 1
  • 12
  • I'm trying to get a `string`, not `json`, anyway i tried your method and it crash at `HttpURLConnection` declaration. – Papanash Sep 20 '17 at 13:30
  • it depends what the web service will give to you . and can you give me the error? do you have a internet permission ? – White Druid Sep 20 '17 at 13:32
  • 1
    call that method or your method on AsyncTask you have a NetworkOnMainThreadException Exception see here pls https://stackoverflow.com/questions/6343166/how-do-i-fix-android-os-networkonmainthreadexception – White Druid Sep 20 '17 at 13:40