简体   繁体   中英

Android application internet connection

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.

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!

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.

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!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM