25520

Android: URL Connection accessing to website

Question:

I am extracting details from a website using the below code.

<h2>Code:</h2> private class FetchAllData extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { super.onPreExecute(); Utilities.custom_toast(CurrentResult.this, "Refreshing", "gone!", "short", "vertical"); } @Override protected Void doInBackground(Void... params) { try { //String urlX = URL1 + "?x=" + new Random().nextInt(100000); //Method1 String urlX = URL1 //Method2; URL url = new URL(""+ urlX); URLConnection con = url.openConnection(); con.setUseCaches(false); //This will stop caching! // BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; PageCode = ""; OriginalPageCode = ""; while ((inputLine = in.readLine()) != null) { PageCode += inputLine; } OriginalPageCode = PageCode; toast_IshtmlObtained = urlX+ "\nHTML success obtained as follows:\n\n"; try { extract_website_and_save(); toast_IsInfoExtracted = "success extracting website"; } catch (Exception e1) { toast_IsInfoExtracted = "error extracting website"; } in.close(); } catch (Exception e) { PageCode = "ERROR: " + e; toast_IshtmlObtained = "HTML not obtained:\nHTML retrieved as follows:" + PageCode; } return null; } @Override protected void onPostExecute(Void result) { Utilities.custom_toast(CurrentResult.this, "Done", "gone!", "short", "vertical"); setText(); ...... } } <h2>Question:</h2>

Beforehand I was using the URL in Method1 and was successful to access and extract the website details. However, in these days it does not work. I now tried Method2 and it works now.

I would like to ask

<ol><li>

if the random number in Method1 is important for preventing caches if the number of users accessing to the website is enormous, and

</li> <li>

what is the drawback if using the direct URL as in Method2? Thanks.

</li> <li>

Would adding under URLConnection con.addHeader("Cache-Control", "no-cache"); solve the problem ?

</li> </ol>

Answer1:

Try to use retrofit2.0 or fast android networking library to connect to external web Apis. it's more readable and easy to use than using AsycTask here are helpful links that will teach you how to use them

<a href="https://github.com/amitshekhariitbhu/Fast-Android-Networking" rel="nofollow">https://github.com/amitshekhariitbhu/Fast-Android-Networking</a> its amazing lib for all external connections

Also, use standard lib for parsing like gson

<a href="http://www.vogella.com/tutorials/JavaLibrary-Gson/article.html" rel="nofollow">http://www.vogella.com/tutorials/JavaLibrary-Gson/article.html</a>

Recommend

  • Getting the ping results using java
  • Parsing a file with single and multi-lines of data
  • AWS Lambda - Buffered reader
  • Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range
  • Upcasting and Overloading Stream Operator
  • Getting external IP address with Java [duplicate]
  • getCodeBase() gives nullpointer in Java Applet
  • Images are not appearing in RCP application after exporting as a product
  • java - Download and then write image as servlet response
  • java - Download and then write image as servlet response
  • Using Java to extract data from google Weather API
  • Load HTML Page In Textarea
  • Android Async loading Images in Listview - images are blinking
  • Strange in ShakeGesture Library on Windows Phone Application
  • PDFReader utility radaee causing application crashed
  • Using a handler / AsyncTask or similar to run a networking task
  • How to make the standard music player find my files
  • Android System.err out of the blue
  • how to read and write in arabic in eclipse
  • org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject
  • How to get jQuery ajax calls to work from greasemonkey page events
  • How To display Parsed Xml data in to ListView in Android
  • How to run shell commands with root privileges in Java
  • how to wait for a process to end in java or clojure
  • getNodeValue() returns null although response xml holder does not
  • BufferedReader readline when reading in a thread
  • Rails + Amazon RDS : latency issues
  • how to handle onLoad Event on img tag and others
  • Java catching exceptions and subclases
  • How can I stop .htaccess redirecting a subdomain to the main site?
  • How to read data from socket connection - android
  • How could I write a BsonDocument object into a file, and read it again, using Java
  • Why == is different for Integer and String?
  • Runtime error in UVA Online Judge [closed]
  • RabbitMQ java client stops consuming messages
  • How can I tell a form not to dispose a particular control when it closes?
  • SonarQube: Cannot deactivate rule with missing quality profile
  • Trying to switch camera back to front but getting exception
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • KeystoneJS: Relationships in Admin UI not updating