JSON提取在真实主机中无法正常工作,但在localhost中工作

时间:2017-12-19 10:11:49

标签: php android android-volley host

我在我的免费托管中有json http://alibhm.epizy.com/fetch_turbine2.php

正如您所见,如果在浏览器中运行此操作并正确获取两个参数。但是当把这个网址放在我的Android应用程序(有排球)时,不要取任何东西。在之前,当我在我的localhost(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);

1 个答案:

答案 0 :(得分:0)

一周后我发现答案:)我的代码没有问题。一切由免费托管保护引起的。一些免费托管有防止机器人的保护。和我的应用程序称为此主机的机器人。我改变主机,一切都像魅力:)。我建议使用付费主机来传递问题。