2

I'm trying to experiment the HttpURLConnection to get some XML from a server. This is the code I'm using:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    String myConn = this.getString(R.string.myConnection);
    HttpURLConnection httpConnection = null;
    InputStream in = null;
    try {
        URL mySite = new URL(myConn);
        URLConnection connection = mySite.openConnection();
        TextView tv = (TextView)this.findViewById(R.id.myTextView);
        httpConnection = (HttpURLConnection)connection;         

            in = httpConnection.getInputStream();

            String myString = convertStreamToString(in);
            tv.setText(myString);           

    } catch (IOException ex) {} catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally {
        httpConnection.disconnect();
        try {
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

On the emulator this code works and i can see the stream on the TextView...

On my device I can't see anything (3g connection)..

Did I miss something?

thanks

4

1 回答 1

-1

通过使用 AsyncTask 可以解决这个问题。

于 2013-05-26T10:06:09.887 回答