读取url to string时什么也得不到

时间:2012-12-13 07:45:32

标签: java android url

我尝试从url中读取json字符串:

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();
    }

然后我使用Toast来显示str1

中的内容
Toast.makeText(getBaseContext(), 
                str1, 
                Toast.LENGTH_LONG).show();

但我一无所获。我得到一个例外,它显示catch2。这些代码在我的Eclipse中的Android应用程序项目中。

当我将这些代码复制并复制到我的普通Java项目时,它可以工作。

新的堆栈跟踪:

        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

2 个答案:

答案 0 :(得分:1)

如果您在Android 3.0+上运行此代码,您可能会获得NetworkOnMainThreadException,因为它看起来像是在主线程上执行此操作。您需要使用AsyncTask来执行此类任务。

你能做的是:

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();            

答案 1 :(得分:0)

也许这取决于您正在构建的Android平台。不是2.3或4.0的相同构建。如果您使用HttpClient构建2.2或低于3.0。 here一个例子。