这是我第一次问这里,我是Android Studio新手。 如何让应用程序显示网站上的内容? 我是从"连接到网络"在Android Studio培训中 https://developer.android.com/training/basics/network-ops/connecting.html 。我尝试过更改网址,但我无法显示除错误消息以外的任何内容 http://i.stack.imgur.com/34TJ8.jpg
编辑:如果有可能我想以这种方式尝试,因为我想知道我在哪里做错了并尝试解决它。
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 :(得分:2)
我认为你对本教程试图说的内容存在误解。教程页面显示了在后台进程中从Internet下载文件的方法,因为不需要向用户显示所有事件。
比如说,你正在制作板球比分应用,你需要从你的网站获得分数。现在,您没有向用户提供分数数据的网页网址,并要求他使用Chrome,Firefox或其他网络浏览器查看它吗?
因此,您将使用HttpURLConnection
在应用中下载该实时得分数据,并使用AsyncTask
方法将此任务保留在主要线程之外应用程序。您可能还会显示一个对话框,其中显示&#34;请稍候...&#34;告知用户这个。 这是案例1
或者您希望 嵌入 Web浏览器,以便在应用程序中直接显示数据。 这是案例2
这里,本教程试图完成案例1场景,而@Ramesh和@Fahad Shah提供的答案提供案例2.
答案 1 :(得分:1)
在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 :(得分: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 WebView教程。 http://www.tutorialspoint.com/android/android_webview_layout.htm
希望这有帮助。
答案 3 :(得分:0)
在xml中使用
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/webView" />
在java活动中
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("your web link");
webView.clearCache(true);
清单中的 <uses-permission android:name="android.permission.INTERNET" />
答案 4 :(得分:0)
要显示来自网址的内容,您可以使用 WebView in Android 可能是您的问题,因为您使用的网址无效或您尚未正确实施,请按照步骤操作如下所示
在androidmenifest.xml中使用此权限询问连接互联网权限
<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我已经制作了一个非常好的代码片段,它肯定会帮助你。