简体   繁体   English

WebView无法使用INTERNET权限

[英]WebView is not working with INTERNET permission

I have a simple application and I cannot make the WebView to show google.com. 我有一个简单的应用程序,但无法制作WebView来显示google.com。

No errors but nothing show on EditText. 没有错误,但在EditText上没有任何显示。

I has added Internet permission already but, it is not work. 我已经添加了Internet权限,但是它不起作用。

This is my Manifest. 这是我的清单。

<?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>

I think there isn't problem in manifest. 我认为清单中没有问题。 I don't know what the problem is exactly. 我不知道问题到底出在哪里。 This is my code. 这是我的代码。

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();
   }

No errors or messages on the LogCat. LogCat上没有错误或消息。 I'm testing this code on my device. 我正在我的设备上测试此代码。

You should not access the network from main thread. 您不应从主线程访问网络。 Use an AsyncTask to perform the access. 使用AsyncTask执行访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM