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
}
}
}
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.