开发者

Problem with getting source code of page

开发者 https://www.devze.com 2023-02-20 21:14 出处:网络
I\'m new in Java and in programming on android. Have a look at this code: TextView tv = new TextView(this);

I'm new in Java and in programming on android. Have a look at this code:

TextView tv = new TextView(this);
URI uri;
HttpResponse response = null;
String str = "dupa2";
try {
    uri = new URI("http://google.com");
    HttpGet get = new HttpGet(uri);
    HttpClient client = new DefaultHttpClient();
    try {
        response = client.execute(get);
        HttpEntity enity = response.getEntity();
        str = enity.toString();
        str = "2";
    } catch (IOException ex) {
        tv.setText("blad");
        Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
    }
} catch (URISyntaxException ex) {
    tv.setText("blad");
    Logger.getLogger(MainActivity.class.getName()).log(Level.SE开发者_如何转开发VERE, null, ex);
}
tv.setText(str);
setContentView(tv);

It doesn't work and i dont have idea why :/ str value is "dupa2" when it should be "2" (i added it because i didnt know whats going on) or "blad") WTF?

Must I add that try...catch statements? Thanks in advance, Chris


It seems that you've an exception thrown, so the tv text is set to "blad" and then again to "dupa2" (the 2nd line from the end).


Definitely you are getting an exception, add into your AndroidManifest.xml internet permission

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

post your stacktrace if this not solve your problem

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号