0

我尝试从 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
4

2 回答 2

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();            
于 2012-12-13T08:39:01.673 回答
0

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

于 2012-12-13T08:42:01.527 回答