user23256
user23256

Reputation: 53

Get nothing when read url to string

I try to read a json string from a url:

String message = "http://cs-server.usc.edu:12695/examples/servlet/Moviefb?title=batman&title_type=feature";

    URL website;
    String str1 = "";

    try {
        website = new URL(message);
        URLConnection connection = website.openConnection();

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

        String inputLine;
        while ((inputLine = in.readLine()) != null) 
            str1 += inputLine;

    } catch (MalformedURLException e1) {
        // TODO Auto-generated catch block
Toast.makeText(getBaseContext(), 
                    "catch1", 
                    Toast.LENGTH_LONG).show();
        e1.printStackTrace();
    } catch (Exception e) {

Toast.makeText(getBaseContext(), 
                "catch2", 
                Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }

And then I use Toastto show what's in str1

Toast.makeText(getBaseContext(), 
                str1, 
                Toast.LENGTH_LONG).show();

But I get nothing. And I got an exception,it shows catch2. These code are in my Android Application Project in Eclipse.

When I copy and past these code to my ordinary Java Project, it works.

the new stacktrace:

        12-13 09:02:28.909: W/System.err(1015): java.lang.SecurityException: Permission denied (missing INTERNET permission?)
12-13 09:02:28.949: W/System.err(1015):     at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
12-13 09:02:29.079: W/Trace(1015): Unexpected value from nativeGetEnabledTags: 0
12-13 09:02:29.149: W/System.err(1015):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
12-13 09:02:29.159: W/System.err(1015):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
12-13 09:02:29.179: W/Trace(1015): Unexpected value from nativeGetEnabledTags: 0
12-13 09:02:29.219: W/Trace(1015): Unexpected value from nativeGetEnabledTags: 0
12-13 09:02:29.239: W/System.err(1015):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
12-13 09:02:29.328: W/Trace(1015): Unexpected value from nativeGetEnabledTags: 0
12-13 09:02:29.428: W/System.err(1015):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
12-13 09:02:29.549: W/System.err(1015):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
12-13 09:02:29.609: W/System.err(1015):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
12-13 09:02:29.669: W/System.err(1015):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
12-13 09:02:29.669: W/System.err(1015):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
12-13 09:02:29.719: W/System.err(1015):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
12-13 09:02:29.819: W/System.err(1015):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
12-13 09:02:29.879: W/System.err(1015):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
12-13 09:02:29.959: W/System.err(1015):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
12-13 09:02:29.959: W/System.err(1015):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
12-13 09:02:29.959: W/System.err(1015):     at com.example.hwnine.Show$1.doInBackground(Show.java:48)
12-13 09:02:30.229: W/System.err(1015):     at com.example.hwnine.Show$1.doInBackground(Show.java:1)
12-13 09:02:30.268: W/System.err(1015):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-13 09:02:30.268: W/System.err(1015):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-13 09:02:30.308: W/Trace(1015): Unexpected value from nativeGetEnabledTags: 0
12-13 09:02:30.308: W/Trace(1015): Unexpected value from nativeGetEnabledTags: 0
12-13 09:02:30.348: W/Trace(1015): Unexpected value from nativeGetEnabledTags: 0
12-13 09:02:30.368: W/System.err(1015):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-13 09:02:30.398: W/Trace(1015): Unexpected value from nativeGetEnabledTags: 0
12-13 09:02:30.479: W/System.err(1015):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-13 09:02:30.489: W/System.err(1015):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-13 09:02:30.499: W/System.err(1015):     at java.lang.Thread.run(Thread.java:856)
12-13 09:02:30.499: W/System.err(1015): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
12-13 09:02:30.519: W/System.err(1015):     at libcore.io.Posix.getaddrinfo(Native Method)
12-13 09:02:30.519: W/System.err(1015):     at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:59)
12-13 09:02:30.529: W/System.err(1015):     at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
12-13 09:02:30.539: W/System.err(1015):     ... 21 more
12-13 09:02:30.539: W/System.err(1015): Caused by: libcore.io.ErrnoException: getaddrinfo failed: EACCES (Permission denied)
12-13 09:02:30.559: W/System.err(1015):     ... 24 more
12-13 09:02:30.589: W/Trace(1015): Unexpected value from nativeGetEnabledTags: 0

Upvotes: 0

Views: 304

Answers (2)

Vladimir
Vladimir

Reputation: 9743

If you run this code on Android 3.0+ you will probably get NetworkOnMainThreadException, since it looks like you do this on the main thread. You need to use AsyncTask for this types of tasks.

What you can do is:

final String message = "http://cs-server.usc.edu:12695/examples/servlet/Moviefb?title=batman&title_type=feature";

new AsyncTask<Void, Void, String>() {
    private String mErrDesc = null;

    @Override
    protected String doInBackground(Void... paramArrayOfParams) {
       URL website;
       String str1 = null;

       try {
           website = new URL(message);
           URLConnection connection = website.openConnection();

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

           String inputLine;
           while ((inputLine = in.readLine()) != null) 
               str1 += inputLine;

        } catch (MalformedURLException e1) {
            mErrDesc = "MalformedURLException thrown";
            e1.printStackTrace();
            return null;
        } catch (Exception e) {
            mErrDesc = "Exception thrown";
            e.printStackTrace();
            return null;
        }

        return str1;
    }

    @Override 
    protected void onPostExecute(String str) {
        if (str != null)
            Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show();
        else if (mErrDesc != null) 
            Toast.makeText(getBaseContext(), mErrDesc, Toast.LENGTH_LONG).show();
        else 
            Toast.makeText(getBaseContext(), "Unknown error", Toast.LENGTH_LONG).show();
    }

}.execute();            

Upvotes: 1

ƒernando Valle
ƒernando Valle

Reputation: 3714

Maybe it depends for what Android platform are you building. Not is the same build for 2.3 or 4.0. If you are building for 2.2 or less than 3.0 use HttpClient. here an example.

Upvotes: 0

Related Questions