-1

I have found a working code for makeing simply HTTP requests here, from How can I make a simple HTTP request in MainActivity.java? (Android Studio) and I am going to post it below (with some changes, if I am not wrong it is now necessery to use try{} catch{}). But I would like to ask how I can receive the content? I work with the code in the following way:

GetUrlContentTask req = new GetUrlContentTask();
req.execute("http://192.168.1.10/?pin=OFF1");
textView3.setText(req.doInBackground("http://192.168.1.10/?pin=OFF1")); 

GetUrlContentTask

private class GetUrlContentTask extends AsyncTask<String, Integer, String> {
    protected String doInBackground(String... urls) {
        // String content1 = "";
        try {
            URL url = new URL(urls[0]);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setDoOutput(true);
            connection.connect();


            BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String content = "", line;
            while ((line = rd.readLine()) != null) {
                content += line + "\n";
            }
            // content1 = content;
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        // return content1; - returns "", wrong
        return "aaa";
        //does not work    return content;
    }

    protected void onProgressUpdate(Integer... progress) {
    }

    protected void onPostExecute(String result) {
        // this is executed on the main thread after the process is over
        // update your UI here    
    }
}
Czarek
  • 689
  • 9
  • 20

2 Answers2

0

Add this to your onPostExecute(String result) call

    protected void onPostExecute(String result) {
        // this is executed on the main thread after the process is over
        // update your UI here    
        setMyData(result);
    }

And then fill your textfield or whatever other data you need to update.

    private void setMyData(String result){
        textView3.setText(result); 
    }
Barns
  • 4,850
  • 3
  • 17
  • 31
0

Where exactly are you stuck? Your code mostly correct although you may need to rearrange it slightly. Your scope is a bit off and your commented code almost gets there. See below

protected String doInBackground(String... urls) {
    String content = "", line = "";
    HttpURLConnection httpURLConnection;
    BufferedReader bufferedReader;
    URL url;

    try {
        for(String uri : urls) {
            url = new URL(uri);
            url = new URI(url.toURI().getScheme(), url.getAuthority(), url.getPath(), "pin=" + URLEncoder.encode("&OFF1", "UTF-8"), null).toURL();

            httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.connect();


            bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
            while ((line = bufferedReader.readLine()) != null) {
                content += line + System.lineSeparator();
            }
        }
     } catch (Exception e) {
         e.printStackTrace();
     } finally {
         try {
            bufferedReader.close();
         } catch(Exception e) {
             e.printStackTrace();
         }
     }

     return content;

}

That will return a String representation of your page. If the page contains HTML it will return text/html, if it contains text it will return just text.

Then as a previous user stated you can set the response on your GUI

protected void onPostExecute(String result) {
    textView3.setText(result);
}

The above example will technically work. However, I would highly recommend using http://loopj.com/android-async-http/ over HttpURLConnection. Your code will be much nicer which will matter down the line.

The code I sent you assumes that the parameter is always pin=OFF1 because it's more a proof of concept

Chris Maggiulli
  • 3,375
  • 26
  • 39
  • The code sets textView3 text on "". What is more there is an error on line: `bufferedReader.close();` "bufferedReader might not have been initialized". If I try `BufferedReader bufferedReader = null;` then I have received unhandled exception in line `bufferedReader.close();` – Czarek Jun 06 '17 at 12:44
  • So the statement in your OP that "I have found a working code for makeing simply HTTP requests" was really not working. The question in your OP has been answered - correctly - You need to open a new question with the problems you mentioned about the error. Add the Error messages to that post. – Barns Jun 06 '17 at 15:48
  • Except that your code doesn't work either and actually doesnt address the question at all if you go back and read it. The issue is actually with the get parameter. The code was working on my local environment because i had no get parameter in my test case and my compilation error settings are configured different. I suggest you go back and look at the issue because your solution doesn't even begin to address the issue he's having which is that the value is returning in a browser but not in his Java program. Read the comments in his original post and second post. – Chris Maggiulli Jun 06 '17 at 16:14
  • I was in no way criticizing your answer! I simply stated the to the OP said(!): "I have found a working code for makeing simply HTTP requests" And that "But I would like to ask how I can receive the content? " an that in no way was this "textView3.setText(req.doInBackground("http://192.168.1.10/?pin=OFF1"));" going to work!! – Barns Jun 06 '17 at 16:19
  • Sorry Czarek I got half way through addressing your issue before I got side tracked. Take a look at the code now and give it a try. I think the issue is with your get parameter. If I understand you correctly you know that that URL with that get parameter returns something in the browser, but its returning empty in your java program. You had a scope issue originally which i thought was the issue. But my test case was wrong I didnt use a get parameter. After your commented I went back and modified the code to include your param differently. sorry about comp err my javac is configured – Chris Maggiulli Jun 06 '17 at 16:19
  • Sorry Barns52 didn't mean to come off as aggressive – Chris Maggiulli Jun 06 '17 at 16:20