我使用模拟器开发android应用程序,使用模拟器中的浏览器可以访问互联网,但我的应用程序不能访问,请高手看看是什么问题,多谢! (android的版本是4.0)
Java代码如下:
----------------------------
- Java code
package com.android.network;import java.net.MalformedURLException;import java.net.URL;import java.io.InputStream;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;public class ReadBytesActivity extends Activity{ private static final String DEBUG_TAG = "ReadBytesActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Log.i(DEBUG_TAG, "application start!"); Button go = (Button)findViewById(R.id.do_action); go.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { URL text = new URL("http://www.liyebei.com/index.html"); InputStream isText = text.openStream(); byte[] bText = new byte[250]; int readSize = isText.read(bText); Log.i(DEBUG_TAG, "read size = " + readSize); Log.i(DEBUG_TAG, "byte text = " + new String(bText)); isText.close(); } catch(MalformedURLException e) { e.printStackTrace(); Log.i(DEBUG_TAG, "the URL cann't be find"); } catch(Exception e) { e.printStackTrace(); Log.i(DEBUG_TAG, "there are some problems"); } } }); } }
对应的Layout代码如下:
-------------------------------------------
- XML code
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/instruct" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Press button for action" /> <Button android:id="@+id/do_action" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Go!" /> </LinearLayout> <TextSwitcher android:id="@+id/status" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <ImageSwitcher android:id="@+id/images" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextSwitcher android:id="@+id/info" android:layout_width="fill_parent" android:layout_height="wrap_content" /></LinearLayout>
--------------------------------------------------------------
对应的Manifest.xml如下:
- XML code