Autore Topic: HttpURLConnection: come ottenere la versione web?  (Letto 588 volte)

Offline Ricky`

  • Moderatore globale
  • Utente storico
  • *****
  • Post: 2696
  • Respect: +370
    • Google+
    • rciovati
    • Mostra profilo
  • Dispositivo Android:
    Galaxy Nexus
  • Sistema operativo:
    OS X Lion
Re:HttpURLConnection: come ottenere la versione web?
« Risposta #15 il: 03 Febbraio 2012, 16:18:14 CET »
0
Perchè passi il BufferedReader al onPostExecute? Dovresti leggere la stringa dentro l'doInBackground e passare solo quella.

Offline Allen

  • Nuovo arrivato
  • *
  • Post: 37
  • Respect: 0
    • Mostra profilo
Re:HttpURLConnection: come ottenere la versione web?
« Risposta #16 il: 03 Febbraio 2012, 16:27:28 CET »
0
Hai ragione!!Adesso va:) Ti ringrazio!!!Scrivo il codice corretto per completezza:
Codice (Java): [Seleziona]
public class PROVAURLActivity extends Activity {

        private String url = "http://www.google.it/";
        private TextView t1;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                t1 = (TextView) findViewById(R.id.editText1);
                new SorgenteUrl().execute(url);
        }

        private class SorgenteUrl extends AsyncTask<String, Void, String> {

                protected String doInBackground(String...strings ) {
                        try {
                                URL myUrl = new URL(url);
                                HttpURLConnection conn = (HttpURLConnection) myUrl
                                                .openConnection();
                                BufferedReader in = new BufferedReader(new InputStreamReader(
                                                conn.getInputStream()));
                                String a=in.readLine().substring(0,5);
                                return a;
                        } catch (MalformedURLException e) {
                                // TODO Auto-generated catch block
                                Log.e("ERRORE","url errore");
                                e.printStackTrace();
                                return null;
                        } catch (IOException e) {
                                // TODO Auto-generated catch block
                                Log.e("ERRORE", "IOException");
                                e.printStackTrace();
                                return null;
                        }
                }

                protected void onPostExecute(String in) {
                        t1.setText(in);
                }

        }

Offline Allen

  • Nuovo arrivato
  • *
  • Post: 37
  • Respect: 0
    • Mostra profilo
Re:HttpURLConnection: come ottenere la versione web?
« Risposta #17 il: 04 Febbraio 2012, 12:39:52 CET »
0
Una curiosità ;-)perchè in quel modo non andava e mettendo la chiamata http in un thread separato funziona? grazie

Offline Ricky`

  • Moderatore globale
  • Utente storico
  • *****
  • Post: 2696
  • Respect: +370
    • Google+
    • rciovati
    • Mostra profilo
  • Dispositivo Android:
    Galaxy Nexus
  • Sistema operativo:
    OS X Lion
Re:HttpURLConnection: come ottenere la versione web?
« Risposta #18 il: 04 Febbraio 2012, 12:56:37 CET »
0
Perchè hai lo StrictMode attivo, che è uno strumento che ti segnala accessi al disco o alla rete seguiti nel thread della user interface e che quindi vanno a rallentare la responsività dell'applicazione:

StrictMode | Android Developers


Offline Allen

  • Nuovo arrivato
  • *
  • Post: 37
  • Respect: 0
    • Mostra profilo
Re:HttpURLConnection: come ottenere la versione web?
« Risposta #19 il: 07 Febbraio 2012, 17:32:10 CET »
0
Se invece di una TextView volessi visualizzare una ListView con gli elementi di una lista restituita nel metodo doInBackground come bisognerebbe fare?