How to request and get a server response code over a Wifi Network?

  • Replies:9
Chaven Yenketswamy
  • Forum posts: 8

Aug 25, 2018, 1:04:25 PM via Website

Hi

Iam looking for suggestions from people that have done something similar.

I basically have a microprocessor board equipped with a Wifi module connected to an Android phone which provides the Access Point. The microprocessor code launches a webserver with a single webpage that displays on my Android application. Ive used a basic webView component. The webpage refreshes every 10 secs showing the status of sensor inputs to the microprocessor.

When a certain trigger is raised on the microprocessor i need this signal accessible inside the Android application. From what ive been reading one needs to use HTTP requests from the Android app (Client) to the webserver and process the response to retrieve the information.

What are the recommended classes that i should use for performing these transactions over Wifi ? It is basically a single integer value that i want to transfer from the microprocessor to the Android app.

Ive tried using HttpClient and HttpsURLConnection and am not making any progress. Ive considered parsing the html code to extract the information but this is rather messy and there must be a more elegant approach.

Reply
James Watson
  • Forum posts: 1,584

Aug 26, 2018, 3:09:11 AM via Website

If the microprocessor provides neither a web service( with some methods ) nor a web api, but only a web server with a single webpage, you will have to parse the web page in your android app code.
But I think that web page should be simple enough and easy to parse.

— modified on Aug 26, 2018, 4:05:27 AM

Download size < 0.15 MB. But also accurate enough, ad-free & free.
The minimalist app available on Play Store: https://goo.gl/ws42fN
Blog: https://okblackcafe.blogspot.com Your 5-star is appreciated.

Helpful?
Reply
Chaven Yenketswamy
  • Forum posts: 8

Aug 26, 2018, 9:46:47 AM via Website

Its a single webpage on a webserver just a few lines of html code.

Struggling to even get a connection that prevents me proceeding any further. Exception is raised when i make the connection. What could cause the exception. The webpage is loading and updating in the WebView.

//NB I have Android Users permission for internet access in the manifest

public class MainActivity extends Activity {

WebView webView;
WebViewClient wvClient=new WebViewClient();
TextView display;
RelativeLayout framelayout; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    framelayout=(RelativeLayout)findViewById(R.id.framelayout);
    webView = (WebView)findViewById(R.id.webView);
    display =(TextView)findViewById(R.id.display);

    webView.loadUrl("xxx.xxx.xx.xx");               
    webView.setWebViewClient(wvClient);
    display.setText("Status bar");

    try{            

        URL url = new URL("xxx.xxx.xx.xx");

        //Step 1: Create our connection object. NB the connection is not made yet until we call connect

        URLConnection urlconn = (URLConnection)url.openConnection();//This creates our connection object to the resource referenced by the url

        /*Step 2: We specify setup parameters
        Available methods are viz: setAllowUserInteraction, setDoInput, setDoOutput
        setIfModifiedSince, setUseCaches*/

        urlconn.setAllowUserInteraction(false);//we dont allow user interaction
        urlconn.setDoInput(true);//This indicates that the application reads data from the URL Connection
        urlconn.setDoOutput(false);//We not writing data to the URL Connection
        urlconn.setIfModifiedSince(0);//We always want fetching to occur
        urlconn.setUseCaches(true);//We want caching where possible instead of getting a fresh copy each time

        //Step 3: We set general request properties using method setRequestProperty
        //Not entirely sure what to put inside this method
        //urlconn.setRequestProperty(?????, ????);

        //Step 4: Look at setConnectTimeout,setReadTimeout 

        urlconn.setConnectTimeout(15000);//if timeout occurs before a connection is established then java.net.SocketTimeoutException is raised
        urlconn.setReadTimeout(15000);//if timeout expires before data is available for read then java.net.SocketTimeoutException is raised

        //Step 5: we create the connection using urlconn.connect() which make the remote object available

        urlconn.connect();//This is causing the exception even if we have an infinite timeout


        //Step 6: We access the header fields and contents of the remote object
            display.setText(urlconn.getHeaderField("h1"));


    }
    catch(Exception ex){
        display.setText("Exception Raised");//So we have an Exception raised

    }

}

}

Helpful?
Reply
James Watson
  • Forum posts: 1,584

Aug 26, 2018, 11:43:00 AM via Website

First of all, make sure that the web server is available for your Android device. You may verify this by browsing the single web page on that web server in the stock web browser app. If it works well, just focus on your app code to debug.

Download size < 0.15 MB. But also accurate enough, ad-free & free.
The minimalist app available on Play Store: https://goo.gl/ws42fN
Blog: https://okblackcafe.blogspot.com Your 5-star is appreciated.

Helpful?
Reply
Chaven Yenketswamy
  • Forum posts: 8

Aug 26, 2018, 1:30:24 PM via Website

Ive been running the app from the emulator inside Eclipse. The webpage
is loading in the emulator. Maybe I need to upload the apk to the mobile device instead. Think I will just type out the same code on AIDE and try again.

Helpful?
Reply
James Watson
  • Forum posts: 1,584

Aug 26, 2018, 2:09:03 PM via Website

I just made a simple app to display a web page. It works fine on my mobile phone. It only needs several rows of codes.

— modified on Aug 26, 2018, 2:11:21 PM

Download size < 0.15 MB. But also accurate enough, ad-free & free.
The minimalist app available on Play Store: https://goo.gl/ws42fN
Blog: https://okblackcafe.blogspot.com Your 5-star is appreciated.

Helpful?
Reply
Chaven Yenketswamy
  • Forum posts: 8

Aug 26, 2018, 2:24:39 PM via Website

Its not working the exception is android.os.NetworkOnMainThreadException. One has to run network operations on a different thread from the UI. You can override this behaviour using StrictMode but that is also giving issues.

Could you post a sample of how you accessed your html code which is what I need access to.

Thank you

Helpful?
Reply
James Watson
  • Forum posts: 1,584

Aug 27, 2018, 3:46:21 AM via Website

Chaven Yenketswamy

Its not working the exception is android.os.NetworkOnMainThreadException. One has to run network operations on a different thread from the UI. You can override this behaviour using StrictMode but that is also giving issues.

Could you post a sample of how you accessed your html code which is what I need access to.

Thank you

Sorry, my app just gets an URL which users have entered and then simply displays the web page. It has not tried to parse any html code.

Download size < 0.15 MB. But also accurate enough, ad-free & free.
The minimalist app available on Play Store: https://goo.gl/ws42fN
Blog: https://okblackcafe.blogspot.com Your 5-star is appreciated.

Helpful?
Reply
Chaven Yenketswamy
  • Forum posts: 8

Aug 29, 2018, 5:07:14 PM via Website

Thanks ive found a solution after much reading and trying out other approaches like Runnable and Handler. Looks like it will only work if you use an asynchronous background task. So i subclassed AsyncTask but used HttpURLConnection which gives you the getresponse method. So i have a response 200 and am able to access all my html code and extract out my needed data.

Helpful?
James Watson
Reply
James Watson
  • Forum posts: 1,584

Aug 30, 2018, 4:23:29 AM via Website

Chaven Yenketswamy

Thanks ive found a solution after much reading and trying out other approaches like Runnable and Handler. Looks like it will only work if you use an asynchronous background task. So i subclassed AsyncTask but used HttpURLConnection which gives you the getresponse method. So i have a response 200 and am able to access all my html code and extract out my needed data.

Cool! The response code 200 means OK. :-) Congratulations again.

Download size < 0.15 MB. But also accurate enough, ad-free & free.
The minimalist app available on Play Store: https://goo.gl/ws42fN
Blog: https://okblackcafe.blogspot.com Your 5-star is appreciated.

Helpful?
Reply