1

I'm new to Android and this is my first application. I'm to connect internet and download JSON from my companies server but unable to get input stream please check this code and provide me assistance.

URL url = new URL("http://www.android.com/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());

URL is not the issue I have tested many URL's. In this code last line gives error. I uses open connectivity (No proxy only firewall) on my development machine and emulators browser is able to access internet.

I have already added

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

parallel to uses-sdk tag in Manifest file.

Saurabh Agarwal
  • 323
  • 2
  • 6
  • 16

3 Answers3

1

try suggesting the server what kind of data you are ready to accept before trying to fect inputstream, if it's json, let the server know you accept data of type application/json, something like

httpcon = (HttpURLConnection) ((new URL(http://www.android.com/).openConnection()));
httpcon.setRequestProperty("Accept", "application/json");

and not to forget the operation you want to perform, GET or POST

httpcon.setRequestMethod("POST"); or  httpcon.setRequestMethod("GET");

Please mention the error/message you are getting so that we know what the server is trying to say!

thepoosh
  • 12,497
  • 15
  • 73
  • 132
stack_ved
  • 721
  • 3
  • 11
0

Use this.

URL url = new URL("http://www.google.com/");
   HttpURLConnection myConnection = (HttpURLConnection) url.openConnection();
   try {
     InputStream in = new BufferedInputStream(myConnection.getInputStream());
     readStream(in);
    finally {
     myConnection.disconnect();
   }
 }

and add User Permission also in your manifest file.

Monty
  • 3,205
  • 8
  • 36
  • 61
0

Here is my working code example..

all you need to Run below code in AsyncTask class and pass your URL and it will returns the response from server.

public String getResult(String url) {

        Log.v(TAG, "Final Requsting URL is : :"+url);

        String line = "";
        String responseData = null;

        try {
            StringBuilder sb = new StringBuilder();
            String x = "";
            URL httpurl = new URL(url);
            URLConnection  tc= httpurl.openConnection();


            /*URLConnection tc = twitter.openConnection();
            HttpURLConnection httpConnection = (HttpURLConnection) tc;
            httpConnection.setRequestMethod("POST");*/
            //tc.setRe
            //tc.setDoOutput(false);

            BufferedReader in = new BufferedReader(
                           new InputStreamReader(tc.getInputStream()));

            while ((line = in.readLine()) != null) {
                sb.append(line + "\n");
                x = sb.toString();
            }
            responseData = new String(x);
        }
        catch (UnknownHostException uh){            
            Log.v("NewWebHelper", "Unknown host :");
            uh.printStackTrace();
        }
       catch (FileNotFoundException e) {
           Log.v("NewWebHelper", "FileNotFoundException :");
            e.printStackTrace();
         }
        catch (IOException e) {
            Log.v("NewWebHelper", "IOException :");
            e.printStackTrace();
        }
      catch (Exception e) {
          Log.v("NewWebHelper", "Exception :");
            e.printStackTrace();
        }
        return responseData;
    }

also make sure you need below Internet permission in your manifest file this way..

<uses-permission android:name="android.permission.INTERNET"/>

Let me know your comment regarding this!!

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
  • Yes I have already added this in Manifest file. For me this code is giving error: android.os.NetworkOnMainThreadException – Saurabh Agarwal Dec 24 '12 at 06:54
  • Yeah Saurabh!! This exception is thrown when an application attempts to perform a networking operation on its main thread. Run your code in AsyncTask, see [this](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) – swiftBoy Dec 24 '12 at 06:57
  • Thanks, Yes this was the only problem. Solved..!! – Saurabh Agarwal Dec 24 '12 at 09:26