Friday, 23 August 2013

android - my app can't access to internet

android - my app can't access to internet

I need to get html content from url.
I create my project in Eclipse(Java ADT) with Android 4.3 SDK and I can't
get HTML content.
But when I create this project in Eclipse(INDIGO) with Android 2.1 SDK , I
can get HTML content!!
Please Help me!
Edited---
My simple project file is this.
I use this code in Androidmanifest.xml:
<uses-permission android:name="android.permission.INTERNET"/>
my code in MainActivity Class:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textview1 = (TextView) findViewById(R.id.textview1);
site getcontent = new site();
try {
String data = getcontent.getData().toString();
textview1.setText(data);
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
textview1.setText("error1");
}
my class for get html content is:
public class site {
public String getData() throws Exception {
BufferedReader in = null;
String data = null;
try {
HttpClient client = new DefaultHttpClient();
URI website = new URI("http://www.google.com/");
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
in = new BufferedReader(new
InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.separator");
while ((l = in.readLine()) != null) {
sb.append(l + nl);
}
in.close();
data = sb.toString();
return data;
} finally {
if (in != null)
try {
in.close();
return data;
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}

No comments:

Post a Comment