php - 如何将变量从 php 发送到 Android 应用程序?

标签 php android

大家好,我当前的 Android 应用程序中有一个 Activity ,其中有一个 Web View 。我想知道如何从该网站上的 php 获取变量并将其存储到我的 android 应用程序上的变量中(是的,我控制该网站,并且具有完整的编辑功能)。

最佳答案

我会解释一般需要做的事情。首先,在你的 Android 应用程序端,应该有类似这样的代码。

1)在你的android端

String url = "www.yoururl.com/yourphpfile.php";
List<NameValuePair> parmeters = new ArrayList<NameValuePair>();
            parameters.add(new BasicNameValuePair("category",category));
            parameters.add(new BasicNameValuePair("subcategory",subcategory));// These are the namevalue pairs which you may want to send to your php file. Below is the method post used to send these parameters

  DefaultHttpClient httpClient = new DefaultHttpClient();

  HttpPost httpPost = new HttpPost(url); // You can use get method too here if you use get at the php scripting side to receive any values.

  httpPost.setEntity(new UrlEncodedFormEntity(parameters));

  HttpResponse httpResponse = httpClient.execute(httpPost);

  HttpEntity httpEntity = httpResponse.getEntity();

  is = httpEntity.getContent();

   BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8); // From here you can extract the data that you get from your php file..

        StringBuilder builder = new StringBuilder();

        String line = null;

        while ((line = reader.readLine()) != null) {
            builder.append(line + "\n");
        }

        is.close();
        json = sb.toString(); // Here you are converting again the string into json object. So that you can get values with the help of keys that you send from the php side.

        String message = json.getString("message");
         int success = json.getInt("success"); // In this way you again convert json object into your original data.

2) 在你的 php 端

$response = array();

// check for post fields
if (isset($_POST['category']) && isset($_POST['subcategory'])) {

    $category = $_POST['category'];
    $subcategory = $_POST['subcategory'];


    // include db connect class
    require_once __DIR__ . '/db_connect.php';

    // connecting to db
    $db = new DB_CONNECT();

    // mysql inserting a new row
    $result = mysql_query("INSERT INTO categorytable(category, subcategory) VALUES('$category', '$subcategory')");

    // check if row inserted or not
    if ($result) {
        // successfully inserted into database
        $response["success"] = 1;
        $response["message"] = "Data inserted into database.";

        // echoing JSON response
        echo json_encode($response);// Here you are echoing json response. So in the inputstream you would get the json data. You need to extract it over there and can display according to your requirement.
} 

正如您所说,您对 php 相当精通,您可能已经发现 php 中的上述内容变得尽可能简单,并且不安全。因此,您可以遵循一些 pdo 或其他一些安全方法在 php 端进行编码。确保将 Android 端的代码包含在 asynctask 中,以便在需要的地方在单独的线程中运行。希望这会有所帮助。

关于php - 如何将变量从 php 发送到 Android 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13635395/

相关文章:

php - 如何在同一个表单中使用两个按钮在ajax中执行不同的操作?

android - 如何为 Android arm64-v8a xcompile Qt?

java - 空闲内存是否在 Android 设备上消耗更少的电量?

java - 延迟数据导致 OnCreate 上出现 NullPointerException

php - 多范式语言

php - 在 Codeigniter 中记录一些数据到数据库

php - 为什么 PHP 会破坏我的 CSS?

php - Json编码带空格的字符串

android - 如何在 Android 中的 RadioButton 上设置 OnClickListener?

java - Android中将json格式的数据转换/解析为java