java - JsonArrayRequest 没有给出任何响应

标签 java php android android-volley

我正在尝试使用 Android Volley 库从远程服务器上的数据库中获取数组。问题是JSONobject没有返回 onResponse 中的任何数据方法。当我尝试显示 JSONobject 中的数据时,应用程序崩溃并且 logcat 显示 NullPointerException。

Java代码如下:

public class AudioFragmentThree extends Fragment {
    View view;
    TextView text;
    String url = "http://www.muftiattaullahmultani.com/android/get_all_bayan.php";
    int count = 0;
    int i = 0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.audio_fragment_three, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, url, null, new Response.Listener<JSONArray>() {
            public void onResponse(JSONArray response) {
                while(count<response.length())
                {
                    JSONObject object= null;
                    try {
                        object = response.getJSONObject(count);
                        String s = object.getString("id") +"  "+ object.getString("topic");

                        text.setText(s);

                        count++;
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                text.setText("ERROR");
            }
        });

        MySingleton.getInstance(getActivity()).addToRequestQueue(jsonArrayRequest);
    }
}

LogCat 如下:

FATAL EXCEPTION: main Process: com.example.mashood.muftiattaullahmultanicom, PID: 22596
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at 
com.example.mashood.muftiattaullahmultanicom.AudioFragmentThree$1.onResponse(AudioFragmentThree.java:81)
        at com.example.mashood.muftiattaullahmultanicom.AudioFragmentThree$1.onResponse(AudioFragmentThree.java:69)
        at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
        at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5910)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)

下面是我的 PHP 代码:

<?PHP include 'database.php';
$query = 'SELECT * FROM audio_bayan ORDER BY no DESC';
$result = $conn->query($query);
$response = array();
while($row = $result->fetch_assoc()) {
    array_push($response,array("id"=>$row['no'],"topic"=>$row['topic']));
} 
echo json_encode($response);
?>

最佳答案

您必须像下面这样定义textview:

Textview text;

text = (TextView) view.findViewById(R.id.textid);

关于java - JsonArrayRequest 没有给出任何响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45485230/

相关文章:

java - junit5 创建临时文件

php - Delphi 7 idHTTP JSON数据空结果

php - MySQL JSON 数据类型是否不利于数据检索的性能?

php - 删除 <code> block 之外的所有空格、换行符和制表符

android - Android 中的可扩展列表

java - 如何在 Java 中比较字符串?

java - 删除部分 XML 声明,编码 ="UTF-8"独立 ="yes"JAXB

android - 为什么我应该使用 OkHttp 而不是 android httpClient 和 AsyncTask

java - 在 war 中访问属性文件

android - 如何只用一个元素填充屏幕?