繁体   English   中英

Android Studio尝试使用Volley向PHP发送数据并检索JSON数组给出错误

[英]Android Studio trying to send data and retrieve json array using volley to php gives out error

我试图使用mysql BETWEEN语句做一个过滤器,所以我将开始和结束日期发送到我的php文件,并获取一个json数组结果放入ListView中。 但是即时通讯收到此错误。 请帮助。 提前Tqvm。

test.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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM