【问题标题】:JSON fetch dosen't work correctly in real host but work in localhostJSON fetch 在真实主机中无法正常工作,但在 localhost 中工作
【发布时间】:2017-12-19 10:11:49
【问题描述】:

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

如您所见,如果在浏览器中运行它并获取两个参数,则获取工作正常。但是当把这个 url 放在我的 android 应用程序(带有 volley)中时,什么都没有。以前当我在我的 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);

【问题讨论】:

  • 你调试了吗? json字符串是否来自请求?
  • 调试看看你得到了什么响应字符串,从那里你可能会知道哪里出了问题
  • 你的意思是在android studio中调试? logcat 的哪一部分显示了这一点?
  • 我认为我的问题来自我的主机,但为什么呢?我不知道 。因为它在我的本地主机中正常工作

标签: php android android-volley host


【解决方案1】:

一周后我找到了答案 :) 我的代码没有问题。一切都是由一些免费的托管保护引起的。一些免费托管具有针对机器人的保护。我的应用程序称为此主机的 bot。我改变了我的主人,一切都像魅力一样工作:)。我建议使用付费主机来传递问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-12
    • 2017-02-16
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 2020-09-29
    • 2019-05-07
    • 1970-01-01
    相关资源
    最近更新 更多