Reputation: 133
It's a straightforward question, but one I haven't been able to figure out: How can I access the internet from an application that utilizes AndEngine (AnalogOnScreenControls). My Activity extends BaseGameActivity
. Specifically, I want to open a URL connection. All I need to do is execute the URL; I don't want to view it in any way. Should I do it through a WebView
, HTTPConnection
, openStream()
-- what? Any help would be greatly appreciated!
Upvotes: 0
Views: 612
Reputation: 133
Issue resolved! I had to run all network operations on a thread separate from the main one (else it will throw a NetworkOnMainThread exception). I don't know why nothing else worked, but this did the trick! Here I'm creating a new thread with the action I want to perform, and then starting it after exceptions are taken care of. I found my answer here
new Thread(new Runnable() {
public void run() {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("your_url");
try {
HttpResponse response = httpClient.execute(httpGet, localContext);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
Upvotes: 1
Reputation: 1118
Uri address = Uri.parse("http://your.link.com");
Intent openlink = new Intent(Intent.ACTION_VIEW, address);
yourActivity.startActivity(openlink);
Upvotes: 1