Android üzerinde internet üzerinden dosya indirme, ayrıştırma ve kullanma
🏗️ URI Oluşturma
// Base URL for the Books API.finalString BOOK_BASE_URL ="https://www.googleapis.com/books/v1/volumes?";// Parameter for the search stringfinalString QUERY_PARAM ="q"; // Parameter to limit search results.finalString MAX_RESULTS ="maxResults"; // Parameter to filter by print typefinalString PRINT_TYPE ="printType"; // Build up the query URI, limiting results to 5 printed books.Uri builtURI =Uri.parse(BOOK_BASE_URL).buildUpon().appendQueryParameter(QUERY_PARAM,"pride+prejudice").appendQueryParameter(MAX_RESULTS,"5").appendQueryParameter(PRINT_TYPE,"books").build();
💌 İndirme İsteğinde Bulunma
privateStringdownloadUrl(String myurl) throws IOException {InputStream inputStream =null;// Only display the first 500 characters of the retrieved// web page content.int len =500;try {URL url =newURL(myurl);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setReadTimeout(10000/* milliseconds */);conn.setConnectTimeout(15000/* milliseconds */);// Start the queryconn.connect();int response =conn.getResponseCode();Log.d(DEBUG_TAG,"The response is: "+ response); inputStream =conn.getInputStream();// Convert the InputStream into a stringString contentAsString =convertInputToString(inputStream, len);return contentAsString;// Close the InputStream and connection } finally {conn.disconnect();if (inputStream !=null) {inputStream.close(); } }🧙♂ Detaylı bilgi için TEMP alanına bakabilirsin
// Reads an InputStream and converts it to a String.publicStringconvertInputToString(InputStream stream,int len) throws IOException, UnsupportedEncodingException {Reader reader =null; reader =newInputStreamReader(stream,"UTF-8");char[] buffer =newchar[len];reader.read(buffer);returnnewString(buffer);}