JSON提取无法在实际主机中正常工作,但可以在本地主机中工作 [英] JSON fetch dosen't work correctly in real host but work in localhost

查看:119
本文介绍了JSON提取无法在实际主机中正常工作,但可以在本地主机中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have this json in my free host : http://alibhm.epizy.com/fetch_turbine2.php

如您所见,如果在浏览器中运行并获取两个参数,则提取工作正常.但是当将此URL放入我的android应用程序(带有凌空)时,不要获取任何内容.以前,当我在localhost(xampp)中使用此json时,一切正常,但是当要释放主机时,出现了此问题

As you seen fetching work correctly if run this in browser and fetch two parameter . But when put this url in my android app (with volley) don't fetch anything . In previous when I use this json in my localhost(xampp) everything work correctly but when going to free host this problem appeared

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);

推荐答案

一个星期后,我找到了答案:)我的代码没有问题.一切都是由一些免费的托管保护引起的.一些免费托管可以保护机器人.和我的这个主机的应用被称为机器人.我改变了主人,一切都像魅力一样工作:).我建议使用付费主机来传递问题.

after one week i found answer :) my code don't have problem . everything cause by some free hosting protection . some free hosting have protection against bots . and my app known as bot for this host . i change my host and everything work like charm :) . i recommend use paid hosts to pass problems.

这篇关于JSON提取无法在实际主机中正常工作,但可以在本地主机中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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