What is wrong with this code?


Last Updated:

  1. MIIISTER NEUGIT

    MIIISTER NEUGIT Member This Topic's Starter

    Joined:
    Nov 12, 2010
    Messages:
    17
    Likes Received:
    0
    I have the following code:


    public static String[] GetCategories(APPCLASS i) throws IOException
    {
    URL url = new URL("http://192.168.1.72/getcategories.php");
    URLConnection conn = url.openConnection();
    InputStream is = conn.getInputStream();

    Writer writer = new StringWriter();

    char[] buffer = new char[1024];
    try {
    Reader reader = new BufferedReader(
    new InputStreamReader(is, "UTF-8"));
    int n;
    while ((n = reader.read(buffer)) != -1) {
    writer.write(buffer, 0, n);
    }
    } finally {
    is.close();
    }
    return writer.toString().split("" +
    "");

    }

    http://192.168.1.72/getcategories.php is an address that does exist in a server on my network. My phone is connected to wifi, and when I go to that address in the browser it brings up the correct information. But when this code is ran in my application, it brings up an error: "Permission Denied (maybe missing INTERNET permission)" How can I fix this?

    EDIT: I discovered that it fails at the line "InputStream is = conn.getInputStream();"
     

    Advertisement
  2. miXer

    miXer Well-Known Member

    Joined:
    Aug 12, 2010
    Messages:
    313
    Likes Received:
    51
    To let your application use internet you need a permission in your andoridmanifest.xml file: <uses-permission android:name="android.permission.INTERNET" />
    That goes before the <application> tag or after.
     
    MIIISTER NEUGIT likes this.

Share This Page

Loading...