0

私は単純なアプリケーションを持っていますが、WebView を作成して google.com を表示することはできません。

エラーはありませんが、EditText には何も表示されません。

すでにインターネット許可を追加しましたが、うまくいきません。

これが私のマニフェストです。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.taxihelperforcustomer"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.taxihelperforcustomer.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>

マニフェストとしては問題ないと思います。問題が正確に何であるかはわかりません。これは私のコードです。

EditText mResult;
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mResult = (EditText)findViewById(R.id.result);

        Button btn = (Button)findViewById(R.id.sendBtn);
        btn.setOnClickListener(new Button.OnClickListener(){

   public void onClick(View v) {
    // TODO Auto-generated method stub

    String html = DownloadHtml("http://www.google.com");
    try{

     mResult.setText(html);

    } catch(HTMLException e) {
     Toast.makeText(v.getContext(), e.getMessage(), Toast.LENGTH_LONG).show();
    }
   }
        });
    }

public String DownloadHtml(String addr){
   StringBuilder googleHtml = new StringBuilder();
   try{
      URL url = new URL(addr);
      HttpURLConnection conn = (HttpURLConnection)url.openConnection();
      if(conn != null){
         conn.setConnectTimeout(10000);
         conn.setUseCaches(false);
         if(conn.getResponseCode() == HttpURLConnection.HTTP_OK){
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "EUC-KR"));
            for(;;)
            {
                String line = br.readLine();
                if(line == null)
                    break;
                googleHtml.append(line + "\n");
             }
          br.close();
       }
        conn.disconnect();
     }
   } catch(Exception ex){
      Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
   }
   return googleHtml.toString();
   }

LogCat にエラーやメッセージはありません。このコードをデバイスでテストしています。

4

1 に答える 1

0

メインスレッドからネットワークにアクセスしないでください。AsyncTask を使用してアクセスを実行します。

于 2013-04-30T18:52:53.590 に答える