除非删除此命令,否则我将获得例外:


  android:targetSdkVersion =“ 15”


我在SO的另一个线程中发现了这一点。

但是,我已经在那里使用targetSdkVersion运行了几天。这是我的代码:

public class MainActivity extends BaseActivity {
    private TextView textView;
    private String url = "http://www.backcountryskiers.com/sac/sac-full.html";
    private ImageView image;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.todaysReport);
        image = (ImageView) findViewById(R.id.dangerRose);

        fetcher task = new fetcher();
        task.execute();
    }

    public static Bitmap getBitmapFromURL(String src) {
        try {
            Log.e("src", src);
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            Log.e("Bitmap", "returned");
            return myBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("Exception", e.getMessage());
            return null;
        }

    }

    class fetcher extends AsyncTask<String, Void, String> {
        private ProgressDialog dialog = new ProgressDialog(MainActivity.this);
        private Document doc = null;
        private Elements content = null;
        private Document parse = null;
        private String results = null;
        private Element dangerRatingImg = null;
        private String dangerRatingSrc = null;
        private Bitmap bimage;

        @Override
        protected String doInBackground(String... params) {

            try {
                // bimage = getBitmapFromURL(drUrl);
                doc = Jsoup.connect(url).get();
                Log.e("Jsoup", "...is working...");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Log.e("Exception", e.getMessage());
            }

            content = doc.select("#content");
            parse = Jsoup.parse(doc.html());
            results = doc.select("#content").outerHtml();

            return results;
        }

        @Override
        protected void onPostExecute(String result) {
            // smooth out the long scrolling...
            textView.setMovementMethod(ScrollingMovementMethod.getInstance());

            // find rating image...
            dangerRatingImg = doc.select("img").first();
            dangerRatingSrc = dangerRatingImg.absUrl("src");
            // Get the rating image
            bimage = getBitmapFromURL(dangerRatingSrc);
            image.setImageBitmap(bimage);
            image.setPadding(10, 10, 10, 10);
            image.setScaleType(ScaleType.FIT_XY);

            // return the summary
            results = parse.select("#reportSummary").outerHtml();
            textView.setText(Html.fromHtml(results));
            textView.setPadding(10, 10, 10, 10);
            // ditch the dialog, it's all loaded.
            dialog.dismiss();

        }

        @Override
        protected void onPreExecute() {
            // before we get the async results show this
            dialog.setMessage("Loading Latest Update from the Sierra Avalanche Center...");
            dialog.show();
        }

    }

}


我已经建立了网络连接,可以看到将targetSdkVersion删除的结果很好...但是我知道那是不对的。谢谢大家。

编辑:

11-09 08:24:55.316: D/dalvikvm(9165): GC_CONCURRENT freed 148K, 3% free 11365K/11655K, paused 12ms+12ms, total 39ms
11-09 08:24:55.410: D/dalvikvm(9165): GC_CONCURRENT freed 331K, 5% free 11484K/11975K, paused 1ms+1ms, total 20ms
11-09 08:24:55.418: E/Jsoup(9165): ...is working...
11-09 08:24:55.558: D/dalvikvm(9165): GC_CONCURRENT freed 465K, 5% free 11506K/12103K, paused 12ms+12ms, total 39ms
11-09 08:24:55.558: E/src(9165): http://www.sierraavalanchecenter.org/sites/default/files/images/danger_icons_and_bars/0_nodangerrate.png
11-09 08:24:55.558: D/AndroidRuntime(9165): Shutting down VM
11-09 08:24:55.558: W/dalvikvm(9165): threadid=1: thread exiting with uncaught exception (group=0x40bc2300)
11-09 08:24:55.566: E/AndroidRuntime(9165): FATAL EXCEPTION: main
11-09 08:24:55.566: E/AndroidRuntime(9165): android.os.NetworkOnMainThreadException
11-09 08:24:55.566: E/AndroidRuntime(9165):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at com.backcountryskiers.avalanche.report.norcal.MainActivity.getBitmapFromURL(MainActivity.java:49)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at com.backcountryskiers.avalanche.report.norcal.MainActivity$fetcher.onPostExecute(MainActivity.java:102)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at com.backcountryskiers.avalanche.report.norcal.MainActivity$fetcher.onPostExecute(MainActivity.java:1)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at android.os.AsyncTask.finish(AsyncTask.java:631)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at android.os.Looper.loop(Looper.java:137)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at android.app.ActivityThread.main(ActivityThread.java:4745)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at java.lang.reflect.Method.invokeNative(Native Method)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at java.lang.reflect.Method.invoke(Method.java:511)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at dalvik.system.NativeStart.main(Native Method)

最佳答案

您正在从getBitmapFromURL呼叫onPostExecute

bimage = getBitmapFromURL(dangerRatingSrc);


onPreExecuteonPostExecute在UI线程上运行。您需要此代码位于doInBackground之内(因为这是在新线程上运行的部分)。



更新:您可以像这样重新组织代码(如注释中所述):

@Override
protected String doInBackground(String... params) {
    try {
        doc = Jsoup.connect(url).get();
        Log.e("Jsoup", "...is working...");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.e("Exception", e.getMessage());
    }

    content = doc.select("#content");
    parse = Jsoup.parse(doc.html());
    results = doc.select("#content").outerHtml();

    // find rating image...
    dangerRatingImg = doc.select("img").first();
    dangerRatingSrc = dangerRatingImg.absUrl("src");
    // Get the rating image
    bimage = getBitmapFromURL(dangerRatingSrc);

    return results;
}

@Override
protected void onPostExecute(String result) {
    // smooth out the long scrolling...
    textView.setMovementMethod(ScrollingMovementMethod.getInstance());
    // Set the rating image
    image.setImageBitmap(bimage);
    image.setPadding(10, 10, 10, 10);
    image.setScaleType(ScaleType.FIT_XY);

    // return the summary
    results = parse.select("#reportSummary").outerHtml();
    textView.setText(Html.fromHtml(results));
    textView.setPadding(10, 10, 10, 10);
    // ditch the dialog, it's all loaded.
    dialog.dismiss();
}

09-06 01:25