php - Android Studio 尝试使用 volley 向 php 发送数据并检索 json 数组时出现错误

标签 php android mysql json

我尝试使用 mysql BETWEEN 语句进行过滤,因此我将开始和结束日期发送到我的 php 文件,并获取要放入 ListView 的 json 数组结果。但是我收到这个错误。请帮忙。提前 Tqvm。

测试.java

public class test extends AppCompatActivity {
private ListView listViewManagerViewReport;

String receiveSpStartDate;
String receiveSpEndDate;

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

    receiveDate();
    listViewManagerViewReport = (ListView)findViewById(R.id.lvManagerViewReportResult);
    sendRequest();
}

public void receiveDate(){
    SharedPreferences preferences = getSharedPreferences("PassingDetails", MODE_PRIVATE);
    receiveSpStartDate = preferences.getString("startDate", "..");
    receiveSpEndDate = preferences.getString("endDate", "..");
}

class CustomListManagerViewReportAdapter extends ArrayAdapter<String> {
    private Activity context;
    private String[] id;

    private String[] name;
    private String[] total;
    private String[] transactionDate;
    private String[] transactionTime;
    private String[] transactionType;

    public CustomListManagerViewReportAdapter(Activity context,String[] id, String[] name, String[] total, String[] transactionDate, String[] transactionTime, String[] transactionType)
    {
        super(context, R.layout.manager_view_report_result_layout, id);

        this.context = context;
        this.id = id;

        this.name = name;
        this.total = total;
        this.transactionDate = transactionDate;
        this.transactionTime = transactionTime;
        this.transactionType = transactionType;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View listViewMVRItem = inflater.inflate(R.layout.manager_view_report_result_layout, null, true);

        TextView textViewid = (TextView)listViewMVRItem.findViewById(R.id.tvMVRid);

        TextView textViewName = (TextView)listViewMVRItem.findViewById(R.id.tvMVRTransactionInventoryName);
        TextView textViewTotal = (TextView)listViewMVRItem.findViewById(R.id.tvMVRTransactionInventoryTotal);
        TextView textViewTransactionDate = (TextView)listViewMVRItem.findViewById(R.id.tvMVRTransactionInventoryDate);
        TextView textViewTransactionTime = (TextView)listViewMVRItem.findViewById(R.id.tvMVRTransactionInventoryTime);
        TextView textViewTransactionType = (TextView)listViewMVRItem.findViewById(R.id.tvMVRTransactionInventoryType);

        textViewid.setText(id[position]);
        textViewName.setText(name[position]);
        textViewTotal.setText("TOTAL : " +total[position]);
        textViewTransactionDate.setText("DATE : " +transactionDate[position]);
        textViewTransactionTime.setText("TIME : " +transactionTime[position]);
        textViewTransactionType.setText("TYPE : " +transactionType[position]);

        return listViewMVRItem;
    }
}


private void sendRequest(){

    String url = ManagerViewReportJsonWorker.ViewReport_URL;

    final String startDate = receiveSpStartDate;
    final String endDate = receiveSpEndDate;

    StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    showJSON(response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(test.this,error.toString(), Toast.LENGTH_LONG).show();
                }
            }){
        @Override
        protected Map<String,String> getParams(){
            Map<String,String> params = new HashMap<String, String>();
            params.put(ManagerViewReportJsonWorker.KEY_STARTDATE,startDate);
            params.put(ManagerViewReportJsonWorker.KEY_ENDDATE,endDate);
            return params;
        }

    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);

}

private void showJSON(String json) {
    ManagerViewReportJsonWorker pj = new ManagerViewReportJsonWorker(json);
    pj.parseJSON();
    CustomListManagerViewReportAdapter cl = new CustomListManagerViewReportAdapter(this,
            ManagerViewAllInventoryJsonWorker.id,
            ManagerViewReportJsonWorker.name,
            ManagerViewReportJsonWorker.total,
            ManagerViewReportJsonWorker.transactionDate,
            ManagerViewReportJsonWorker.transactionTime,
            ManagerViewReportJsonWorker.transactionType);
    listViewManagerViewReport.setAdapter(cl);

}
}

php文件

<?php

$con = mysqli_connect(HOST,USER,PASS,DB);

if($_SERVER['REQUEST_METHOD']=='POST'){

$startDate = $_POST['poststartdate'];
$endDate = $_POST['postenddate'];


$sql = "SELECT * FROM inventorytransaction WHERE transactiondate BETWEEN $startDate AND '$endDate'";

$res = mysqli_query($con,$sql);

$result = array();

while($row = mysqli_fetch_array($res))
{
array_push($result,array(
        'id'=>$row[0],
        'name'=>$row[1],
        'total'=>$row[2],
        'transactiondate'=>$row[3],
        'transactiontime'=>$row[4],
        'transactiontype'=>$row[5]));
}

echo json_encode(array("result"=>$result));

mysqli_close($con);

} 
?>

错误日志

11-26 16:50:23.060 7800-7800/com.example.user.inventorymanagement E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                Process: com.example.user.inventorymanagement, PID: 7800
                                                                                java.lang.NullPointerException: storage == null
                                                                                    at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
                                                                                    at java.util.Arrays.asList(Arrays.java:155)
                                                                                    at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:137)
                                                                                    at com.example.user.inventorymanagement.test$CustomListManagerViewReportAdapter.<init>(test.java:0)
                                                                                    at com.example.user.inventorymanagement.test.showJSON(test.java:134)
                                                                                    at com.example.user.inventorymanagement.test.access$000(test.java:25)
                                                                                    at com.example.user.inventorymanagement.test$1.onResponse(test.java:107)
                                                                                    at com.example.user.inventorymanagement.test$1.onResponse(test.java:104)
                                                                                    at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
                                                                                    at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
                                                                                    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:148)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

最佳答案

当适配器的数组之一为空时,会发生此错误。检查它们是否非空,然后才将它们分配给适配器。

class CustomListManagerViewReportAdapter extends ArrayAdapter<String> {
private Activity context;
private String[] id;

private String[] name = new String[] {""};
private String[] total = new String[] {""};
private String[] transactionDate = new String[] {""};
private String[] transactionTime = new String[] {""};
private String[] transactionType = new String[] {""};

public CustomListManagerViewReportAdapter(Activity context,String[] id, String[] name, String[] total, String[] transactionDate, String[] transactionTime, String[] transactionType)
{
    super(context, R.layout.manager_view_report_result_layout, id);

    this.context = context;
    this.id = id;

    if(name != null)
        this.name = name;
    if(total != null)
        this.total = total;
    if(transactionDate != null)
        this.transactionDate = transactionDate;
    if(transactionTime != null)
        this.transactionTime = transactionTime;
    if(transactionType != null)
        this.transactionType = transactionType;
}

关于php - Android Studio 尝试使用 volley 向 php 发送数据并检索 json 数组时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40820781/

相关文章:

android - 如何在 Android 中获取相对于另一个 View 的 View 位置?

android - MediaPlayer 认为 aac* 音频文件是视频文件?

android - android中的过滤器位置错误

mysql - MySQL变量中的多个值

mysql - 内连接3表,检索if条件

php - UTF8 character_set_server 和 DataTables 插件

php - 重定向 PHP

php - 成员(member)和管理员的不同重定向

php - Linkedin 如何获取连接的电子邮件地址

php - 如何MySQL两个日期相加并与当前时间戳进行比较