The Problem:
In my Android app, I needed to make a simple call to a REST
endpoint. I created a separate class extending the AysyncTask class like I am supposed
to and as far as I was concerned, my code was clean and compiled just fine. Unfortunately,
a runtime error occurred when getInputStream() was executed. It jumped right to my “finally” block and the
process ended suddenly.
My Research:
I added printStackTrace() to my finally block. When I
caught and examined the stack trace, this is what I found:
SecurityException: Permission
denied (missing INTERNET permission?)
The Solution:
After extensive googling, I realized I needed to add a
setting to my AndroidManifest.xml file:
<uses-permission
android:name="android.permission.INTERNET" />
After running debugger, I finally made a successful htttp
request. Here’s my code.
String json =
"";
try {
MyRequests
mr = new MyRequests();
mr.execute("http://www.myendpoint.com");
json =
mr.get();
}
catch
(Exception ex)
{
ex.printStackTrace();
}
setContentView(R.layout.activity_name);
TextView tv =
(TextView) findViewById(R.id.lblResponse);
tv.setText(json);
More Reading:
http://stackoverflow.com/questions/17360924/securityexception-permission-denied-missing-internet-permission
Comments
Post a Comment