android 连接 网络

每周精粹 发布于 2012/07/11 10:28
阅读 6K+
收藏 0

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
   
    <ImageView
        android:id="@+id/imageview"
        android:layout_height="wrap_content"
       
        android:layout_width="wrap_content"
        android:contentDescription="@string/app_name"
        />   
   
</RelativeLayout>

代码:

package com.example.url_img;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ImageView;


public class MainActivity extends Activity {
    //private Gallery myGallery;
   
    private ImageView myimageview;
   
    private  String myimageurl ="http://img1.3lian.com/img2011/w11/1122/23/18.jpg" ;
   

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myimageview=(ImageView)findViewById(R.id.imageview);
        setContentView(R.layout.activity_main);
      
        try {
            URL url = new URL(myimageurl);
           
            try {
                URLConnection conn= url.openConnection();
               
                conn.connect();
              
                InputStream is =conn.getInputStream();
               
                Bitmap bitmap=BitmapFactory.decodeStream(is);
                myimageview.setImageBitmap(bitmap);
                is.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
           
       
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
   
}

配置:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.url_img"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="15" />
  
 
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

为什么 会出现:

[2012-07-11 10:21:34 - URL_img] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.url_img/.MainActivity }
这个问题 。

加载中
1
红薯
红薯
你贴出来的所谓问题只是一个调试信息而已,不是错误信息哦
1
u
ulyssesjason
froyo以后就不能在主线程里开网络连接比如socket了,所以会有那个strictmode提示,froyo之前有的,所以要开一个另外的线程,或者用fileserverasynctask这样的类来初始化
0
每周精粹
每周精粹
07-11 02:33:06.100: E/AndroidRuntime(1120): Caused by: android.os.NetworkOnMainThreadException
07-11 02:33:06.100: E/AndroidRuntime(1120):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
07-11 02:33:06.100: E/AndroidRuntime(1120):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
07-11 02:33:06.100: E/AndroidRuntime(1120):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
07-11 02:33:06.100: E/AndroidRuntime(1120):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
07-11 02:33:06.100: E/AndroidRuntime(1120):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
07-11 02:33:06.100: E/AndroidRuntime(1120):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
07-11 02:33:06.100: E/AndroidRuntime(1120):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
07-11 02:33:06.100: E/AndroidRuntime(1120):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
07-11 02:33:06.100: E/AndroidRuntime(1120):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
07-11 02:33:06.100: E/AndroidRuntime(1120):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
07-11 02:33:06.100: E/AndroidRuntime(1120):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
07-11 02:33:06.100: E/AndroidRuntime(1120):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
07-11 02:33:06.100: E/AndroidRuntime(1120):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
07-11 02:33:06.100: E/AndroidRuntime(1120):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
07-11 02:33:06.100: E/AndroidRuntime(1120):     at com.example.url_img.MainActivity.onCreate(MainActivity.java:37)
07-11 02:33:06.100: E/AndroidRuntime(1120):     at android.app.Activity.performCreate(Activity.java:5008)
07-11 02:33:06.100: E/AndroidRuntime(1120):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-11 02:33:06.100: E/AndroidRuntime(1120):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
07-11 02:33:06.100: E/AndroidRuntime(1120):     ... 11 more
07-11 02:33:09.670: I/Process(1120): Sending signal. PID: 1120 SIG: 9

0
迷途d书童
迷途d书童

你在代码MainActivity.onCreate(MainActivity.java:37)行出,调用网络连接时间长了,主线程被阻塞。建议使用异步线程处理。

每周精粹
每周精粹
也就是说 ,思路 是 没错 的 是 主要是 线程 被阻塞!!改异步处理 是吧!!
0
每周精粹
每周精粹

引用来自“迷途d书童”的答案

你在代码MainActivity.onCreate(MainActivity.java:37)行出,调用网络连接时间长了,主线程被阻塞。建议使用异步线程处理。

谢谢
0
每周精粹
每周精粹

1:在UI线程中启动一个线程,让这个线程去下载图片。

 2:图片完成下载后发送一个消息去通知UI线程

 3UI线程获取到消息后,更新UI

0
每周精粹
每周精粹
07-11 07:57:18.641: I/Choreographer(1898): Skipped 165 frames!  The application may be doing too much work on its main thread.

0
每周精粹
每周精粹
07-11 08:07:45.181: D/gralloc_goldfish(2049): Emulator without GPU emulation detected.

返回顶部
顶部