Good day,
There is a task to get a list of html strings in the Android application. (in PHP, this is solved by simply calling filegetcontent () and you don’t need to think about anything :) I read that this can be done only by creating a separate stream: Android, I can’t get the html page code
Made absolutely separate classes:
class HTMLGet extends AsyncTask<String,Void,Void> { public BufferedReader reader; @Override public void onPreExecute() { //Обновление интерфейса до начала получения html } @Override public Void doInBackground(String... params) { //HTTP запрос try{ URL url = null; url = new URL("http://google.com"); URLConnection con1 = url.openConnection(); BufferedReader reader = new BufferedReader(new InputStreamReader(con1.getInputStream())); } catch (Exception e){ e.printStackTrace(); } return null; } @Override public void onPostExecute(Void result) { //Обновление интерфейса после получения html try{ String line =""; while ((line=reader.readLine())!=null){ System.out.printf(line); } } catch (Exception e){ e.printStackTrace(); } } } And a description of mainactivity:
public class MainActivity extends Activity { public void onmyClick(View view) throws IOException { HTMLGet htmlGet = new HTMLGet(); String URL="http://google.com"; //Параметр передаю но пока код его не использует htmlGet.execute(URL); Toast.makeText(this, "Page connect!", Toast.LENGTH_SHORT).show(); view.playSoundEffect(android.view.SoundEffectConstants.CLICK); } } The project is perfectly compiled on Eclipse but does not give the desired result. nothing is displayed: (in this connection, the question of how to correctly encode a separate stream, where it should be located () relative to MineActivity, to get html strings by url ... and in passing a question that was suggested to me: how to transfer BufferedReader reader inside a class HTMLGet as well as TextEdit3 (from MainActivity) for example in the HTMLGet class that the actually loaded html code is written there (instead of prinf) ...
HTMLGet.readerfieldHTMLGet.readernot initialized (in thedoInBackgroundyou create a local variable with the same name), so when it comes toonPostExecuteeverything dies with NPE. - zRrr