-2

我正在尝试以编程方式拍摄谷歌搜索的第一张图片,并将其放入我的 android 应用程序中。我找到了一个为我制作的示例(此处:https ://developers.google.com/image-search/v1/jsondevguide?hl=it#using_json )但我在不寻常部分之前遇到了问题(使用的部分json)...这是我的代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    WebView webview = new WebView(this);
    setContentView(webview);
    String s = "ciao";

try {
        URL strana = new URL("https://ajax.googleapis.com/ajax/services/search/images?"
        + "v=1.0&q=barack%20obama&userip=INSERT-USER-IP");
        HttpURLConnection urlConnection = (HttpURLConnection) strana
                .openConnection();
        urlConnection.setDoOutput(true);
        String line;
        StringBuilder builder = new StringBuilder();
        InputStreamReader isr = new InputStreamReader(
                urlConnection.getInputStream());
        BufferedReader reader = new BufferedReader(isr);
        while ((line = reader.readLine()) != null) {
            builder.append(line);
        }
        JSONObject json = new JSONObject(builder.toString());
        s = json.getJSONObject("responseData").getJSONArray("results")
                .getJSONObject(0).getString("url");
    } catch (Exception ex) {
        ex.printStackTrace();
    }


    webview.getSettings().setJavaScriptEnabled(true);
    String summary = "<html><body>You scored <b>192</b> points." + s
            + "<div></div></body></html>";
    webview.loadData(summary, "text/html", null);

}

我对“urlConnection.getInputStream()”行有问题,这是应用程序在捕获异常之前处理的最后一行,但我不明白为什么。我尝试使用 URLConnection 代替 HttpURLConnection (正如我之前链接的参考文献中所做的那样),但没有任何改变。我还添加了“urlConnection.setDoOutput(true);”行 希望有一些变化,但什么都没有。我已将这一行添加到清单中

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

所以这不是问题。这是logcat:

06-25 09:31:05.840: W/IInputConnectionWrapper(850): showStatusIcon on inactive InputConnection
06-25 09:31:13.975: I/webclipboard(1340): clipservice: android.sec.clipboard.ClipboardExManager@41f2e1c8
06-25 09:31:14.030: D/ApplicationPackageManager(1340): Inside getCSCPackageItemIcon
06-25 09:31:14.040: V/webkit(1340): BrowserFrame constructor: this=Handler (android.webkit.BrowserFrame) {41f288a8}
06-25 09:31:14.080: W/System.err(1340): android.os.NetworkOnMainThreadException
06-25 09:31:14.080: W/System.err(1340):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
06-25 09:31:14.080: W/System.err(1340):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
06-25 09:31:14.080: W/System.err(1340):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
06-25 09:31:14.080: W/System.err(1340):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
06-25 09:31:14.080: W/System.err(1340):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
06-25 09:31:14.080: W/System.err(1340):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
06-25 09:31:14.080: W/System.err(1340):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
06-25 09:31:14.080: W/System.err(1340):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
06-25 09:31:14.080: W/System.err(1340):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
06-25 09:31:14.080: W/System.err(1340):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
06-25 09:31:14.080: W/System.err(1340):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461)
06-25 09:31:14.080: W/System.err(1340):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
06-25 09:31:14.080: W/System.err(1340):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
06-25 09:31:14.080: W/System.err(1340):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
06-25 09:31:14.085: W/System.err(1340):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
06-25 09:31:14.085: W/System.err(1340):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
06-25 09:31:14.085: W/System.err(1340):     at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271)
06-25 09:31:14.085: W/System.err(1340):     at myapp.test.imgseeker.MainActivity.onCreate(MainActivity.java:45)
06-25 09:31:14.085: W/System.err(1340):     at android.app.Activity.performCreate(Activity.java:5206)
06-25 09:31:14.085: W/System.err(1340):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
06-25 09:31:14.085: W/System.err(1340):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
06-25 09:31:14.085: W/System.err(1340):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
06-25 09:31:14.085: W/System.err(1340):     at android.app.ActivityThread.access$700(ActivityThread.java:140)
06-25 09:31:14.085: W/System.err(1340):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
06-25 09:31:14.085: W/System.err(1340):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-25 09:31:14.085: W/System.err(1340):     at android.os.Looper.loop(Looper.java:137)
06-25 09:31:14.085: W/System.err(1340):     at android.app.ActivityThread.main(ActivityThread.java:4921)
06-25 09:31:14.085: W/System.err(1340):     at java.lang.reflect.Method.invokeNative(Native Method)
06-25 09:31:14.085: W/System.err(1340):     at java.lang.reflect.Method.invoke(Method.java:511)
06-25 09:31:14.085: W/System.err(1340):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
06-25 09:31:14.085: W/System.err(1340):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
06-25 09:31:14.085: W/System.err(1340):     at dalvik.system.NativeStart.main(Native Method)
06-25 09:31:14.105: D/WebView(1340): loadUrlImpl: called
06-25 09:31:14.110: D/webcore(1340):  CORE loadUrl: called
06-25 09:31:14.110: D/webkit(1340): Firewall not null
06-25 09:31:14.110: D/webkit(1340): euler: isUrlBlocked = false
06-25 09:31:14.160: I/GATE(1340): <GATE-M>DEV_ACTION_COMPLETED</GATE-M>
06-25 09:31:14.190: D/libEGL(1340): loaded /system/lib/egl/libEGL_mali.so
06-25 09:31:14.195: D/libEGL(1340): loaded /system/lib/egl/libGLESv1_CM_mali.so
06-25 09:31:14.205: D/libEGL(1340): loaded /system/lib/egl/libGLESv2_mali.so
06-25 09:31:14.210: D/(1340): Device driver API match
06-25 09:31:14.210: D/(1340): Device driver API version: 10
06-25 09:31:14.210: D/(1340): User space API version: 10 
06-25 09:31:14.210: D/(1340): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Thu Oct 25 08:43:05 KST 2012 
06-25 09:31:14.250: D/dalvikvm(1340): GC_CONCURRENT freed 250K, 14% free 9538K/11015K, paused 16ms+2ms, total 38ms
06-25 09:31:14.260: D/OpenGLRenderer(1340): Enabling debug mode 0
06-25 09:31:14.265: D/WebView(1340): onSizeChanged - w:480 h:690
06-25 09:31:14.390: D/TilesManager(1340): new EGLContext from framework: 514be150 
06-25 09:31:14.390: D/GLWebViewState(1340): Reinit shader
06-25 09:31:14.430: D/GLWebViewState(1340): Reinit transferQueue
4

1 回答 1

1

抛出的异常是NetworkOnMainThreadException. 这是为了防止开发人员将他们的网络代码放在 UI 线程中,这可能会冻结 UI,因为它会阻塞直到请求完成。

如果你想做一个网络请求,你应该使用一个AsyncTask这样你的 UI 线程可以继续运行。这是一个示例:使用 Android 下载文件,并在 ProgressDialog 中显示进度

于 2013-06-25T07:46:16.733 回答