php - JSON 获取在真实主机中无法正常工作,但在本地主机中工作

标签 php android android-volley host

我的免费主机中有这个 json:http://alibhm.epizy.com/fetch_turbine2.php

如您所见,如果在浏览器中运行它并获取两个参数,获取工作将正常进行。但是当把这个 url 放在我的 android 应用程序中时(带截击)不要获取任何东西。以前,当我在本地主机 (xampp) 中使用此 json 时,一切正常,但在释放主机时出现此问题

dbconfig.php

<?php
//Define your host here.
$servername = "xxx";
//Define your database username here.
$username = "xxx";
//Define your database password here.
$password = "*******";
//Define your database name here.
$dbname = "xxx";
?>

fetch_turbine2.php

<?php 

include 'dbconfig.php';

$con = mysqli_connect($servername,$username,$password,$dbname);
 //creating a query
 $stmt = $con->prepare("SELECT POWER , ONE_THRUST_BEARING_METAL_TEMP

 FROM turbine_table ORDER BY id DESC LIMIT 1;");

 //executing the query 
 $stmt->execute();

 //binding results to the query 
 $stmt->bind_result($t1,$t2);

 $boiler = array(); 

 //traversing through all the result 
 while($stmt->fetch()){
 $temp = array();

$temp['t1'] = $t1;
$temp['t2'] = $t2;

 array_push($boiler, $temp);
 }

 //displaying the result in json format 
 echo json_encode($boiler);
 ?>

Java

StringRequest stringRequest1=new StringRequest(Request.Method.GET,"http://alibhm.epizy.com/fetch_turbine2.php",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONArray array=new JSONArray(response);
                        for (int i=0; i < array.length(); i++) {
                            JSONObject product=array.getJSONObject(i);

                            String b1=product.getString("t1");
                            String b2=product.getString("t2");

                            final TextView a1=findViewById(R.id.pf1);
                            final TextView a2=findViewById(R.id.pf2);

                            a1.setText(b1);
                            a2.setText(b2);

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            });
    Volley.newRequestQueue(this).add(stringRequest1);

最佳答案

一周后我找到了答案 :) 我的代码没有问题。一切都是由一些免费托管保护引起的。一些免费托管可以防止机器人。我的应用程序称为此主机的机器人。我换了我的主人,一切都很有魅力:)。我建议使用付费主机来传递问题。

关于php - JSON 获取在真实主机中无法正常工作,但在本地主机中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47856541/

相关文章:

php - 使用 PHP 的 DOM 获取 div 的内容

android - 如何在 Android 中以编程方式设置谷歌字体

安卓 JNI 错误 : warning: initialization makes integer from pointer without a cast

android - 我如何告诉 Android Volley 中的 TLS 版本

java - 设置请求优先级 Volley

android - 致命异常 : Thread-18 java. lang.OutOfMemoryError:在使用较低版本设备的 android volley 中

php - 什么是 "Resource#' s”?

javascript - 您可以使用 HTML 将文本输入作为文件上传吗?

php - PDO 插入查询减慢网站速度

android - 无法获取未知属性 'localProperties'