ANDROID&PHP - 如何使用 PHP 从 MySql 显示 JSONArray [英] ANDROID&PHP - How to display JSONArray from MySql using PHP

查看:55
本文介绍了ANDROID&PHP - 如何使用 PHP 从 MySql 显示 JSONArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用来将表的值显示到下面的 JSONArray 中的代码

The code that I use to displaying value of table into JSONArray below

send_data.php

<?php
include 'dbconfig.php';
$con = new mysqli($servername, $username, $password, $dbname);
if (mysqli_connect_errno())
  {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

  $query = "select id,ask from pertanyaan";

  $result = mysqli_query($con,$query);

  $rows = array();
  while($r = mysqli_fetch_array($result)) {
    $rows[] =$r;
  }
echo json_encode(array_values($rows));

  mysqli_close($con);
?>

JSONArray 输出就像 this

and the JSONArray output is like this

[{"0":"1","id":"1","1":"pertanyaan ke 1","ask":"pertanyaan ke 1"},{"0":"2","id":"2","1":"pertanyaan ke 2","ask":"pertanyaan ke 2"},{"0":"3","id":"3","1":"pertanyaan ke 3","ask":"pertanyaan ke 3"},{"0":"4","id":"4","1":"pertanyaan ke 4","ask":"pertanyaan ke 4"},{"0":"5","id":"5","1":"pertanyaan ke 5","ask":"pertanyaan ke 5"}]

但是每次我尝试在 android 中显示该 URL 时,我都会收到一条错误消息

but everytime i try that URL to display in android, i got an error that say

Value

但是当我创建具有相同输出的新 API here 到 android studio 时,它的工作正常,

but when i create new API with the same output here into android studio, its work fine,

我是否使用错误的代码从 PHP 编码 JSON?

did i use wrong code to encode JSON from PHP?

推荐答案

问题

HttpURLConnection 不支持 JavaScript,但使用 JavaScript 生成所需的 cookie.

HttpURLConnection has no JavaScript support, but a needed cookie is generated using JavaScript.

您的来电

String reqUrl = "http://zxccvvv.cuccfree.com/send_data.php";
URL url = new URL(reqUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

失败,因为 cookie __test 丢失.

fails, because the cookie __test is missing.

修复

乍一看 JavaScript 源代码,对于给定的 url,cookie 似乎是常量,因此为 常量 cookie 设置可能就足够了:

From a first glance at the JavaScript source the cookie seems to be constant for a given url, so it might be enough to set for a constant cookie:

String cookie = "__test=2bf7c15501c0ec57f8e41cb84871a69c";

URL url = new URL(reqUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(7000);
conn.setRequestMethod("GET");
conn.setRequestProperty("Cookie", cookie);

替代方案:我们可以使用 WebView 获取 cookie,因此这是更可取的方法,因为它不会中断,如果 cookie 发生变化并且不会有太多时间延迟:>

Alternative: Using a WebView we can grab the cookie, so this is the preferable approach, since it will not break, if the cookie changes and it is not much of a time delay:

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

    getCookie();

    if(cookie!=null) {
        new GetContacts().execute();
    }
}

private void getCookie(){
    CookieManager.setAcceptFileSchemeCookies(true);
    WebView wv = new WebView(getApplicationContext());
    wv.getSettings().setJavaScriptEnabled(true);
    wv.loadUrl(url);
    cookie = CookieManager.getInstance().getCookie("zxccvvv.cuccfree.com");
}

并按照上面的例子进行设置:

and set it as in above example:

conn.setRequestProperty("Cookie", cookie);

logcat 中的输出

Response from url: [{"0":"1","id":"1","1":"pertanyaan ke 1","ask":"pertanyaan ke 1"},{"0":"2","id":"2","1":"pertanyaan ke 2","ask":"pertanyaan ke 2"},{"0":"3","id":"3","1":"pertanyaan ke 3","ask":"pertanyaan ke 3"},{"0":"4","id":"4","1":"pertanyaan ke 4","ask":"pertanyaan ke 4"},{"0":"5","id":"5","1":"pertanyaan ke 5","ask":"pertanyaan ke 5"}]

这篇关于ANDROID&amp;amp;PHP - 如何使用 PHP 从 MySql 显示 JSONArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆