Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

con.getInputStream() throw exception in Android project, how to fix?

Tags:

java

android

con.getInputStream() throw exception, when the code running on android, but when copy the code to regular java project it is working,
The thrown object e contain "detailMessage = Permission denied". my code::

            public static HttpData get(String sUrl) {
                HttpData ret = new HttpData();
                String str;
                StringBuffer buff = new StringBuffer();
                try {
                        URL url = new URL(sUrl);
                        URLConnection con = url.openConnection();
                        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                        while ((str = in.readLine()) != null) {
                                buff.append(str);
                        }
                        ret.content = buff.toString();

                        //get headers
                        Map<String, List<String>> headers = con.getHeaderFields();
                        Set<Entry<String, List<String>>> hKeys = headers.entrySet();
                        for (Iterator<Entry<String, List<String>>> i = hKeys.iterator(); i.hasNext();) {
                                Entry<String, List<String>> m = i.next();

                                Log.w("HEADER_KEY", m.getKey() + "");
                                ret.headers.put(m.getKey(), m.getValue().toString());
                                if (m.getKey().equals("set-cookie"))
                                ret.cookies.put(m.getKey(), m.getValue().toString());
                        }
                } catch (Exception e) {
                        Log.e("HttpRequest", e.toString());
                }
                return ret;
        }

con object while debug

like image 647
kimo Avatar asked Feb 02 '26 23:02

kimo


1 Answers

Make sure you get <uses-permission android:name="android.permission.INTERNET"/> and not <permission android:name="android.permission.INTERNET"/>! That bit me for an hour...

like image 161
Scott Avatar answered Feb 05 '26 12:02

Scott