Getting Excception at httpConn.connect(); (the 5th last line of the code) Code and Exeption Errors are given below..I an trying to connect the android application to internet..I got this code from internet..but it is giving exception..The code is completely given bewlow..i have added permission to manifest already i dont know what is te erroe
Code..
public class httpsData extends Activity {
ImageView img;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String str = DownloadText("http://www.edumobile.org/android/");
TextView txt = (TextView) findViewById(R.id.text);
txt.setText(str);
}
private String DownloadText(String URL)
{ Log.v("Wasim ","11");
int BUFFER_SIZE = 2000;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return "";
}
InputStreamReader isr = new InputStreamReader(in);
int charRead;
String str = "";
char[] inputBuffer = new char[BUFFER_SIZE];
try {
while ((charRead = isr.read(inputBuffer))>0)
{
//---convert the chars to a String---
String readString = String.copyValueOf(inputBuffer, 0, charRead);
str += readString;
inputBuffer = new char[BUFFER_SIZE];
}
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "";
}
return str;
}
private InputStream OpenHttpConnection(String urlString)
throws IOException
{
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
{
throw new IOException("Not an HTTP connection");
}
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
return in;
}
}
Error are these:
12-04 14:57:34.947: E/AndroidRuntime(5851): FATAL EXCEPTION: main
12-04 14:57:34.947: E/AndroidRuntime(5851): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.apps.httpsData/com.apps.httpsData.httpsData}: android.os.NetworkOnMainThreadException
12-04 14:57:34.947: E/AndroidRuntime(5851): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
12-04 14:57:34.947: E/AndroidRuntime(5851): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-04 14:57:34.947: E/AndroidRuntime(5851): at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-04 14:57:34.947: E/AndroidRuntime(5851): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-04 14:57:34.947: E/AndroidRuntime(5851): at android.os.Handler.dispatchMessage(Handler.java:99)
12-04 14:57:34.947: E/AndroidRuntime(5851): at android.os.Looper.loop(Looper.java:137)
12-04 14:57:34.947: E/AndroidRuntime(5851): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-04 14:57:34.947: E/AndroidRuntime(5851): at java.lang.reflect.Method.invokeNative(Native Method)
12-04 14:57:34.947: E/AndroidRuntime(5851): at java.lang.reflect.Method.invoke(Method.java:511)
12-04 14:57:34.947: E/AndroidRuntime(5851): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-04 14:57:34.947: E/AndroidRuntime(5851): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-04 14:57:34.947: E/AndroidRuntime(5851): at dalvik.system.NativeStart.main(Native Method)
12-04 14:57:34.947: E/AndroidRuntime(5851): Caused by: android.os.NetworkOnMainThreadException
12-04 14:57:34.947: E/AndroidRuntime(5851): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
12-04 14:57:34.947: E/AndroidRuntime(5851): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
12-04 14:57:34.947: E/AndroidRuntime(5851): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
12-04 14:57:34.947: E/AndroidRuntime(5851): at java.net.InetAddress.getAllByName(InetAddress.java:214)
12-04 14:57:34.947: E/AndroidRuntime(5851): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
12-04 14:57:34.947: E/AndroidRuntime(5851): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
12-04 14:57:34.947: E/AndroidRuntime(5851): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
12-04 14:57:34.947: E/AndroidRuntime(5851): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
12-04 14:57:34.947: E/AndroidRuntime(5851): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
12-04 14:57:34.947: E/AndroidRuntime(5851): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
12-04 14:57:34.947: E/AndroidRuntime(5851): at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
12-04 14:57:34.947: E/AndroidRuntime(5851): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
12-04 14:57:34.947: E/AndroidRuntime(5851): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
12-04 14:57:34.947: E/AndroidRuntime(5851): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
12-04 14:57:34.947: E/AndroidRuntime(5851): at com.apps.httpsData.httpsData.OpenHttpConnection(httpsData.java:97)
12-04 14:57:34.947: E/AndroidRuntime(5851): at com.apps.httpsData.httpsData.DownloadText(httpsData.java:42)
12-04 14:57:34.947: E/AndroidRuntime(5851): at com.apps.httpsData.httpsData.onCreate(httpsData.java:30)
12-04 14:57:34.947: E/AndroidRuntime(5851): at android.app.Activity.performCreate(Activity.java:5008)
12-04 14:57:34.947: E/AndroidRuntime(5851): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-04 14:57:34.947: E/AndroidRuntime(5851): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-04 14:57:34.947: E/AndroidRuntime(5851): ... 11 more
Try this..