【问题标题】:How to show a PHP content in a Android TextView?如何在 Android TextView 中显示 PHP 内容?
【发布时间】:2019-01-25 15:47:45
【问题描述】:

我是新来的,但作为一名程序员,我在 android 中还是个新手。 不幸的是,我的搜索没有成功:(

我想在 TextView 中显示 index.php 文件的所有内容。 这可能吗?

到目前为止我的代码:

res/layout/activity_php.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="@color/colorPrimaryDark" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_back"
            android:layout_width="40dp"
            android:layout_height="match_parent"
            android:background="@drawable/ic_back" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textAlignment="center"
            android:text="@string/title_php"
            android:gravity="center_vertical"
            android:textSize="20dp"
            android:textColor="@color/colorAccent" />

    </LinearLayout>

    <RelativeLayout
        android:id="@+id/view_php"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="30dp"
        android:layout_marginBottom="30dp" >

        <TextView
            android:id="@+id/view_php_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Here I wanna see the index.php content" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_alignParentBottom="true" >

        <Button
            android:id="@+id/btn_php"
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:layout_centerInParent="true"
            android:text="Show content"
            android:textColor="@color/colorPrimaryDark"
            android:background="@color/colorAccent"
            android:textSize="8dp" />

    </RelativeLayout>

</RelativeLayout>

java/projectName/functions/PhpActivity

package com.packageName.projectName;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class PhpActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_php);

        Button btnBack = findViewById(R.id.btn_back);
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent closeView = new Intent(PhpActivity.this, MainActivity.class);
                startActivity(closeView);
                finish();
            }
        });

        Button btnPHP = findViewById(R.id.btn_php);
        btnPHP.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Here I wanna put a trigger to start the process
                Toast.makeText(TestaActivity.this, "Recieving", Toast.LENGTH_LONG).show();
            }
        });

    }
}

php 文件的路径是这样的: http://www.myDomainName.com/chat/log/index.php

并添加

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

关于 app/manifests/AndroidManifest.xml 文件

PHP有一些HTML内容和MySQL查询结果

<?php
  require_once("head.php");
  require_once("loging-logic.php");

  if(userLoged()) {
    echo "<p class='text-success'>You're logged <?= userLogged() ?>. <a href='logout.php'>Logout?</a></p>";
  } else {
    header("Location: botID.php");
    die();
  }
?>

  <h1>Log Page</h1>

<?php
  require_once("conect.php");
  require_once("body.php");
?>

  <table class="table table-striped table-bordered">

    <?php
      $chatLogs = logList($conDB);
      foreach($chatLogs as $log) :
    ?>

    <tr>
        <td><?= $device['devName'] ?></td>
        <td><?= $type['id'] ?></td>
        <td><?= substr($log['description'], 0, 60) ?></td>
        <td>
          <form action="delete.php" method="post">
            <input type="hidden" name="id" value="<?=$type['id']?>" />
            <button class="btn btn-danger">delete</button>
          </form>
        </td>
    </tr>

    <?php
        endforeach
    ?>

  </table>

<?php
  require_once("foot.php");
?>

如果可能的话并解释一下,我是新手。 提前谢谢!

【问题讨论】:

  • 你没有做任何谷歌搜索吗?如何从网上下载是相当标准的。而且例子不胜枚举。只需阅读一些 stackoverflow 页面。
  • 显然您的回答指向了我正在寻找的解决方案。我会花一些时间在上面,但我会在完成后通知你。感谢您的帮助!
  • @greenapps 感谢您提供所有这些链接。但由于我是菜鸟,他们都没有给出令人满意的答案。不幸的是,绝大多数只显示了一段代码,HttpURLConnection 即使有文档(个人意见),对于初学者来说也不容易。但并非所有内容都丢失了,在我发现 OkHttp 的链接之一中。我花了几个小时来了解如何在我的项目中启用它并找到一个完整的例子。但现在我很高兴,一切都在按应有的方式进行:)
  • 我在这里找到了一个适合我的示例:http://www.zoftino.com/android-okhttp-example
  • 很好。太好了,您继续并找到了解决方案。

标签: php android html


【解决方案1】:

您可以简单地使用 Webview 并像这样加载您的网址

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

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.example.com");

【讨论】:

  • 我想看代码。喜欢
    ...
猜你喜欢
  • 2015-06-13
  • 1970-01-01
  • 1970-01-01
  • 2014-09-21
  • 2014-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多