【问题标题】:Getting JSON data from a PHP server从 PHP 服务器获取 JSON 数据
【发布时间】:2018-08-29 13:56:02
【问题描述】:

我正在尝试从我在 wamp 服务器中使用 select.php 查询的表中获取 json 数据,数据显示在浏览器中,但由于某种原因,android 无法下载数据。

select.php

    <?php

$conn=mysqli_connect("localhost","root","", "shady") or die("Unable to connect");

if(mysqli_connect_error($conn)) {
    echo "Failed To Connect";
}

$qry = "SELECT * FROM `wellden`";

$res = mysqli_query($conn, $qry);

if ($res) {

    while ($row = mysqli_fetch_array($res)) {
        $flag[] = $row;
    }

    // returns tthe json data
echo json_encode($flag);


}


mysqli_close($conn);        


?>

工作正常并在浏览器中打印数据:

但由于某些奇怪的原因,android 似乎无法从 php 文件中获取 回显数据。

安卓代码:

public class ListOfCourses extends AppCompatActivity {

ListView listView;
ArrayList<String> arrayList;
ArrayAdapter<String> arrayAdapter;
String REG_URL;



public class Listdata extends AsyncTask<String, Void, String>{


    HttpURLConnection conn;
    URL url;
    String jsonStringForMe;

    @Override
    protected String doInBackground(String... strings) {

        // MOST CRITICAL PART.....................................


        String s = strings[0];

        try {

            url = new URL(s);

            conn = (HttpURLConnection) url.openConnection();
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line = null;

            // Read Server Response
            while ((line = reader.readLine()) != null) {
                // Append server response in string
                sb.append(line);
            }

            // Stroring it to use in the onPostExecute Cause we do not have any Button in this view
            jsonStringForMe = sb.toString();
            return  jsonStringForMe;





        }catch (Exception e){

            return e.getMessage();
        }


        // MOST CRITICA PART ENDS HERE..............................

    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // JsonParsing Happens Here
        Toast.makeText(getApplicationContext(), jsonStringForMe+" ", Toast.LENGTH_LONG).show();

    }
}

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

    Listdata listdata = new Listdata();

    REG_URL="http://1.0.0.2/android_db_pool/select.php";
    listdata.execute(REG_URL);



}
}

但每次 jsonStringForMe 在 postExecute 中给我 null 一切似乎都可以正常工作,但由于某种原因不起作用。 android端收不到json数据可能是什么原因?

编辑 我还添加了互联网权限:

AndroidManifest.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.slimshady.jamalsir1">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ListOfCourses"></activity>
    </application>

</manifest>

编辑 2

似乎 ip 地址可能是个问题,但插入不能与 insert.php 一起正常工作,我可以使用我的服务器的 ip 地址 1.0 的 ip 地址获取 json 数据。 0.2。在 android 中,我使用服务器的 IP 地址而不是 localhost 在仅 php 我使用 localhost 因为 php 文件在我的服务器中。

【问题讨论】:

  • 您是否在清单中添加互联网权限?
  • 除了moxGeek所说的,你应该做一个jsonObjectRequest,使用volley
  • 是的,我确实添加了允许我编辑的权限
  • 检查并确保代码中的 URL 指向正确的 IP 地址 通常在 localhost 的情况下使用 10.0.2.2
  • 我已经重新配置了 apache 服务器插入与 insert.php 一起工作正常,这不是 IP 地址问题,请让我更新问题

标签: android json android-asynctask


【解决方案1】:

如前所述;

将它存储在onPostExecute 中使用因为我们没有任何 此视图中的按钮

您没有在onPostExecute() 方法中添加或使用获取json ,但您已在onPreExecute 中添加了Toast

尝试添加onPostExecute(),然后显示Toast

受保护的无效onPreExecute()

在之前的 UI 线程上运行 doInBackground(Params...)

https://developer.android.com/reference/android/os/AsyncTask#onPreExecute%28%29

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-20
    • 2012-07-19
    • 2016-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多