0

I'm writing an app that needs to send an http requests to fetch data from a server.

when pressing the button, I the error I pasted in this pastebin: https://pastebin.com/shZwCP8F

the code I use is, in order of execution:

// Triggered when user clicks button
public void btn_click(View v) throws IOException {
    //create and send new http request
    Request req = new Request();
    req.url = "http://localhost/wijndisplay";
    System.out.println(req.send(null));
}

and the Request class I wrote to handle HTTP requests

public class Request {
    public String url;

    private HttpClient client;
    private HttpGet get;
    private HttpResponse response;

    public String send(Map<String, String> data) throws IOException {
        StringBuilder getStr = new StringBuilder("?");

        if(data != null) {
            for (Map.Entry<String, String> entry : data.entrySet()) {
                getStr.append(entry.getKey())
                    .append("=")
                    .append(entry.getValue())
                    .append("&");
            }
            url += getStr.toString();
        }

        client = new DefaultHttpClient();
        get = new HttpGet(url);
        response = client.execute(get);

        if(response.getStatusLine().getStatusCode() != 200) {
            throw new IOException("Failed to get server response");
        }

        return EntityUtils.toString(response.getEntity());
    }
}

I know it's happening while making the request, I've set the internet permissions for the app of course, I've checked if the server is configured correctly and I can't find anything wrong.

any help is appreciated.

Grey
  • 877
  • 9
  • 29

0 Answers0