-2

Edit: I'm extremely sorry for how poorly I asked my question at first, I was extremely frustrated and tired but I know that really isn't an excuse. Anyway, I edited it. I have also already looked a lot on the StackOverflow forums and other ones and tried the methods on their, without result so I guess this isn't really a duplicate question.

After hours of trying and using several methods I finally gave up and decided to post the question here. So frustrating that I can't get this to work, it's for a home automation project and learning new languages like C and python was never a problem while working on this project so not getting this to work is extremely frustrating. Anyway here is the code:

public void buttonOnClick(View v) {
    Button button = (Button) v;
    ((Button) v).setText("clicked");
        // The request here
        // The request here
        // The request here
        // The request here

        }

This is a simple application with one button that should send a simple http get request to 192.168.0.150/main_light/switch. I know this is supposed to work since I've always used HTTP GETs in Tasker for this (and it works in Python). I really hope someone can help me out here, thank you!

P.S. I know there are stil useless actions in here, I'm a JAVA Noob and decided just to not mess with anything until I can finally get this working.

JanG
  • 33
  • 9
  • 3
    Instead of posting a lot of irrelevant code that has nothing to do with firing the HTTP GET request, you should rather show what code you have tried in order to fire the request. – Janus Varmarken Jan 28 '16 at 22:10
  • 1
    Try the answer from here [make an http request with android](http://stackoverflow.com/questions/3505930/make-an-http-request-with-android) – Radu Ionescu Jan 28 '16 at 22:11
  • @JanusVarmarken I'm extremely sorry, I have edited the question. – JanG Jan 29 '16 at 06:48

3 Answers3

0

Try using http://square.github.io/okhttp/

Put the code here like this:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           OkHttpClient client = new OkHttpClient();
           String run(String url) throws IOException {
           Request request = new Request.Builder()
               .url(url)
               .build();

           Response response = client.newCall(request).execute();
           return response.body().string();
        }
    }
});
eriuzo
  • 1,687
  • 1
  • 14
  • 16
0

Use HttpURLConnection to achieve the same:

String serverURL = "http://192.168.0.150/main_light/switch";
URL url = new URL(serverURL);
HttpURLConnection connection = null;
try {
    connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    BufferedReader in = new BufferedReader(
    new InputStreamReader(con.getInputStream()));
    //Do something with this InputStream
    // handle the response
    int status = connection.getResponseCode();
    // If response is not success
    if (status != 200) {
        throw new IOException("Post failed with error code " + status);
    }
} catch (Exception e) {
    Log.e(TAG,e.toString());
} finally {
     if(connection != null)
         connection.disconnect();
}

Hope this helps!

TeChNo_DeViL
  • 739
  • 5
  • 11
  • Of course this will not help as you are not reading the page that is sent by the server. So your code `gets` nothing. – greenapps Jan 28 '16 at 22:21
  • Oops, missed out the most cruial thing. Thanks for pointing out. – TeChNo_DeViL Jan 28 '16 at 22:27
  • Your code still does not retrieve the page. – greenapps Jan 29 '16 at 11:21
  • I get an ANDROID.OS.NETWORKONMAINTHREADEXCPETION which probably means I should run it as an assync task I guess? – JanG Jan 29 '16 at 21:40
  • I got it to work using an async task, really stupid that I didn't think of it before since I had already tried this exact method. Anyway, thanks a lot answering my question considering how poorly it was asked. Have a nice day! – JanG Jan 29 '16 at 21:47
0

Use this method:

public static int get(String url) throws Exception {
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("GET");
    int responseCode = con.getResponseCode();
    BufferedReader in = new BufferedReader(
        new InputStreamReader(con.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null) {
        //consume response if any;
    }
    in.close();
    return responseCode;
}

Implement the consumption of the request if you need to.

Ulises
  • 9,115
  • 2
  • 30
  • 27