2016-09-04 167 views
0

这是我第一次问这里,我是Android Studio的新手。 如何让应用程序显示网站上的内容? 我是从Android Studio中训练中的“连接到网络”学习 https://developer.android.com/training/basics/network-ops/connecting.html .I've试图改变URL,但我不能得到它比我的错误信息 http://i.stack.imgur.com/34TJ8.jpg如何让Android应用程序显示网站上的内容?

编辑显示其他任何:如果可能的话,我想尝试这样做,因为我想知道我在这里做错了什么,并试图解决它。

public class MainActivity extends AppCompatActivity { 
    private static final String DEBUG_TAG = "HttpExample"; 
    private TextView textView; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
      Snackbar.make(view, "Connected to network", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 



private String downloadURL(String myurl) throws IOException{ 
    InputStream is = null; 

    try{ 
     URL url = new URL(myurl); 

     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
     conn.setReadTimeout(10000); 
     conn.setConnectTimeout(15000); 
     conn.setRequestMethod("GET"); 
     conn.setDoInput(true); 

     conn.connect(); 
     int response = conn.getResponseCode(); 
     Log.d(DEBUG_TAG, "The response is: " + response); 
     is = conn.getInputStream(); 

     String contentAsString = readIt(is); 
     return contentAsString; 
    }finally { 
     if(is != null){ 
      is.close(); 
     } 
    } 
} 
private class DownloadWebPageTask extends AsyncTask<String,Void, String> { 
    @Override 
    protected String doInBackground(String... urls){ 
     try{ 
      return downloadURL(urls[0]); 
     }catch (IOException e){ 
      return "Unable to retrieve web page. URL may be invalid."; 
     } 
    } 
    @Override 
    protected void onPostExecute(String result) { 
     textView = (TextView) findViewById(R.id.display); 
     textView.setText(result); 
    } 
} 
public String readIt (InputStream is)throws IOException, UnsupportedEncodingException { 
    BufferedReader reader = 
      new BufferedReader(new InputStreamReader(is, "UTF-8")); 
    StringBuilder builder = new StringBuilder(); 

    String input; 
    while ((input = reader.readLine()) != null) 
     builder.append(input); 

    return builder.toString(); 
} 

public void myClickHandler (View view){ 
    ConnectivityManager connMgr = (ConnectivityManager) 
      getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); 

    if (networkInfo != null && networkInfo.isConnected()){ 
     Snackbar.make(view, "Connected to network", Snackbar.LENGTH_LONG) 
       .setAction("Action", null).show(); 
     new DownloadWebPageTask().execute("http://www.google.com"); 
    } 
} 

}

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:context="com.fullcreamlowfat.wirelessp5q1.MainActivity" 
tools:showIn="@layout/activity_main"> 

<TextView android:id="@+id/display" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Hello World!" 
    android:onClick="myClickHandler"/> 
</RelativeLayout> 
+0

使用此XML中

<WebView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/webView" /> 

在Java活动

webView = (WebView) findViewById(R.id.webView); webView.getSettings().setJavaScriptEnabled(true); webView.loadUrl("your web link"); webView.clearCache(true); //If you will not use this method url links are opeen in new brower not in webview public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } //Show loader on url load public void onLoadResource (WebView view, String url) { if (progressDialog == null) { // in standard case YourActivity.this progressDialog = new ProgressDialog(fixture.this); progressDialog.setMessage("Loading..."); progressDialog.show(); } } public void onPageFinished(WebView view, String url) { try{ if (progressDialog.isShowing()) { progressDialog.dismiss(); progressDialog = null; } }catch(Exception exception){ exception.printStackTrace(); } } }); webView.loadUrl(url); 

<uses-permission android:name="android.permission.INTERNET" />你可以在另外的异常日志,以返回一个字符串,指出 “无法挽回”。然后你会知道实际问题 –

回答

2

<uses-permission android:name="android.permission.INTERNET" />我觉得你有什么关于本教程是想说一个误区。教程页面展示了一种在后台进程中从互联网下载文件的方式,因为不需要向用户显示所有事件。

比方说,你是一个板球即时比分应用程序,你需要从你的网站得到分数。现在,您不会将分数数据的网页网址提供给用户,并要求他使用Chrome,Firefox或其他网络浏览器查看它?

因此,你会下载,使用HttpURLConnection生活应用内的得分数据,并会利用AsyncTask方法保持此任务关闭应用程序的主线程。您可能还会显示一个对话框,其中显示“请稍等...”以通知用户这一点。 THIS IS CASE 1

或者你想嵌入 Web浏览器直接显示数据,是在应用程序中。 这是案例2

在这里,本教程试图完成情况1情况,而@Ramesh和@Fahad Shah提供的答案提供情况2。

+0

谢谢。我正在尝试做案例1。 –

0

可以使用的WebView来显示HTML内容或直接加载URL。

webView = (WebView) findViewById(R.id.webView1); 
    webView.loadData(YOUR_HTML, "text/html", "UTF-8"); // which you got from response 

webView.loadUrl(YOUR_URL); 

的Android web视图教程。 http://www.tutorialspoint.com/android/android_webview_layout.htm

希望这会有所帮助。在XML

<WebView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/webView" /> 

0

在Java中使用活动

webView = (WebView) findViewById(R.id.webView); 
     webView.getSettings().setJavaScriptEnabled(true); 
     webView.loadUrl("your web link"); 
     webView.clearCache(true); 

在清单

0

要显示网页URL的内容,你可以使用WebView in Android可能是你的问题是你正在使用的URL无效,或者你有没有做过正确实施,请遵循的步骤,下面写

在你androidmenifest .XML使用此权限要求连接Internet权限

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

,然后在XML文件中使用的WebView组件

<WebView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/web" /> 

和Java方面,

webView = (WebView) findViewById(R.id.web); 
webView.getSettings().setJavaScriptEnabled(true); 
webView.loadUrl("your web link"); 

使用this Link我已经取得了相当不错的代码片断它一定会帮助你。

1

在清单

相关问题