我如何解决volly中的一些距离数据

时间:2019-04-02 22:30:03

标签: android android-volley

亲爱的。 我写了一个代码,用Volly从Web服务中获取数据,但是当我想在一天之内显示数据时,我的Web服务只给我一个响应,我的代码就被捕获了,但是当我选择6天时我的代码正在运行,并且当我的Web服务仅发送诸如此类的内容时[]

[01]*

这是我的数据解析器

public void GetPail(final IMessageListener listener,final String StartDate,final String EndDate){

    Response.Listener listener1 = new Response.Listener() {
        @Override
        public void onResponse(Object response) {
            listener.onSuccess(response.toString());
        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            listener.onFail(error.getMessage());
        }
    };

    StringRequest stringRequest = new StringRequest(Request.Method.POST,URLs.PAIL,listener1,errorListener){
        @Override
        protected Map<String,String> getParams(){
            Map<String,String> params = new HashMap<>();

            params.put("StartDate",StartDate);
            params.put("EndDate",EndDate);

            return params;
        }
    };

    queue.add(stringRequest);
}

这是我的方法

public static List<Pail> GetPails(String response) throws JSONException {

    List<Pail> pailList = new ArrayList<>();

    JSONArray jsonArray = new JSONArray(response);

    for (int i=0 ; i < jsonArray.length() ; i++){

        JSONObject object = jsonArray.getJSONObject(i);

        Pail pail = new Pail();

        String Date = object.getString("Date").substring(0,10).replaceAll("-","/");

        pail.setDate(Date);
        pail.setLimeStone(object.getString("LimeStone"));
        pail.setShail(object.getString("Shail"));
        pail.setSilica(object.getString("Silica"));
        pail.setIron(object.getString("Iron"));
        pail.setGypsum(object.getString("Gypsum"));
        pail.setFilteredLimeStone(object.getString("FilteredLimeStone"));
        pail.setPercentage(object.getString("Percentage"));
        pail.setTotal(object.getString("Total"));

        pailList.add(pail);

    }

    return pailList;
}

这是我的活动方式

public class Pail {

private String Date;
private String LimeStone;
private String Shail;
private String Silica;
private String Iron;
private String Gypsum;
private String FilteredLimeStone;
private String Percentage;
private String Total;


public String getDate() {
    return Date;
}

public void setDate(String date) {
    Date = date;
}

public String getLimeStone() {
    return LimeStone;
}

public void setLimeStone(String limeStone) {
    LimeStone = limeStone;
}

public String getShail() {
    return Shail;
}

public void setShail(String shail) {
    Shail = shail;
}

public String getSilica() {
    return Silica;
}

public void setSilica(String silica) {
    Silica = silica;
}

public String getIron() {
    return Iron;
}

public void setIron(String iron) {
    Iron = iron;
}

public String getGypsum() {
    return Gypsum;
}

public void setGypsum(String gypsum) {
    Gypsum = gypsum;
}

public String getFilteredLimeStone() {
    return FilteredLimeStone;
}

public void setFilteredLimeStone(String filteredLimeStone) {
    FilteredLimeStone = filteredLimeStone;
}

public String getPercentage() {
    return Percentage;
}

public void setPercentage(String percentage) {
    Percentage = percentage;
}

public String getTotal() {
    return Total;
}

public void setTotal(String total) {
    Total = total;
}
}

这是我的RecyclerView显示布局

private void getPail(String StartDate, String EndDate) {
    GetData data = new GetData();

    IMessageListener listener = new IMessageListener() {
        @Override
        public void onSuccess(String Message) {

            try {

                pailList = DataParser.GetPails(Message);

                if (pailList.size() > 0) {


                    PailAdapter adapter = new PailAdapter(pailList);
                    recyclerView.setAdapter(adapter);

                    LinearLayoutManager manager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
                    recyclerView.setLayoutManager(manager);


                    app.SnackBar(recyclerView, "اطلاعات با موفقیت دریافت شد");

                } else {
                    app.SnackBar(recyclerView, "در این بازه زمانی دیتایی موجود نیست");
                }

            } catch (JSONException e) {
                e.printStackTrace();
                app.SnackBar(recyclerView, "مشکلی در دریافت اطلاعات به وجود آمده است.");
            }

        }

        @Override
        public void onFail(String ErrorMessage) {
            if (ErrorMessage == null) {
                app.SnackBar(recyclerView, "مشکل در برقراری ارتباط با سرور\n لطفا اتصال اینترنت خود را بررسی کنید. ");
            }
            //app.SnackBar(recyclerView,ErrorMessage);
        }
    };

    data.GetPail(listener, StartDate, EndDate);
}

这是一天内我的网络服务数据,我的工作不起作用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"
android:layout_height="wrap_content"

android:orientation="vertical"

android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:paddingBottom="50dp"

>


<RelativeLayout
    android:background="@drawable/remain_background"
    android:padding="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">













    <LinearLayout
        android:id="@+id/PailRowHeaderContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/glass_rounded_background"
        android:orientation="horizontal">

        <android.support.v7.widget.AppCompatTextView


            android:id="@+id/PailRowDateTitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:layout_weight="1"
            android:gravity="center"

            android:text="@string/Date"
            android:textColor="@color/TitleTextColor"
            android:textSize="20sp" />



        <android.support.v7.widget.AppCompatTextView

            android:id="@+id/PailRowLimeStoneTitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:layout_weight="1"
            android:gravity="center"

            android:text="@string/LimeStonePersian"
            android:textColor="@color/TitleTextColor"
            android:textSize="20sp" />


        <android.support.v7.widget.AppCompatTextView

            android:id="@+id/PailRowShailTitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:layout_weight="1"
            android:gravity="center"

            android:text="@string/Shail"
            android:textColor="@color/TitleTextColor"
            android:textSize="20sp" />



    </LinearLayout>





    <LinearLayout
        android:layout_marginTop="5dp"
        android:id="@+id/PailRowDetailContainer1"
        android:layout_below="@+id/PailRowHeaderContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <android.support.v7.widget.AppCompatTextView


            android:id="@+id/PailRowDate"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:layout_weight="1"
            android:gravity="center"

            android:text="@string/SampleDate"
            android:textColor="@color/DetailTextColor"
            android:textSize="20sp" />



        <android.support.v7.widget.AppCompatTextView

            android:id="@+id/PailRowLimeStone"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:layout_weight="1"
            android:gravity="center"

            android:text="@string/Zero"
            android:textColor="@color/DetailTextColor"
            android:textSize="20sp" />

        <android.support.v7.widget.AppCompatTextView

            android:id="@+id/PailRowShail"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:layout_weight="1"
            android:gravity="center"

            android:text="@string/Zero"
            android:textColor="@color/DetailTextColor"
            android:textSize="20sp" />





    </LinearLayout>

</RelativeLayout>






<RelativeLayout
    android:background="@drawable/remain_background"
    android:layout_marginTop="10dp"
    android:padding="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">













    <LinearLayout
        android:id="@+id/PailRowHeaderContainer2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/glass_rounded_background"
        android:orientation="horizontal">


        <android.support.v7.widget.AppCompatTextView

            android:id="@+id/PailRowSilicaTitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:layout_weight="1"
            android:gravity="center"

            android:text="@string/Silica"
            android:textColor="@color/TitleTextColor"
            android:textSize="20sp" />



        <android.support.v7.widget.AppCompatTextView

            android:id="@+id/PailRowIronTitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:layout_weight="1"
            android:gravity="center"

            android:text="@string/IronPersian"
            android:textColor="@color/TitleTextColor"
            android:textSize="20sp" />



        <android.support.v7.widget.AppCompatTextView

            android:id="@+id/PailRowGypsumTitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:layout_weight="1"
            android:gravity="center"

            android:text="@string/GypsumPersian"
            android:textColor="@color/TitleTextColor"
            android:textSize="20sp" />









    </LinearLayout>





    <LinearLayout
        android:layout_marginTop="5dp"
        android:id="@+id/PailRowDetailContainer2"
        android:layout_below="@+id/PailRowHeaderContainer2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">


        <android.support.v7.widget.AppCompatTextView

            android:id="@+id/PailRowSilica"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:layout_weight="1"
            android:gravity="center"

            android:text="@string/Zero"
            android:textColor="@color/DetailTextColor"
            android:textSize="20sp" />



        <android.support.v7.widget.AppCompatTextView

            android:id="@+id/PailRowIron"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:layout_weight="1"
            android:gravity="center"

            android:text="@string/Zero"
            android:textColor="@color/DetailTextColor"
            android:textSize="20sp" />



        <android.support.v7.widget.AppCompatTextView

            android:id="@+id/PailRowGypsum"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:layout_weight="1"
            android:gravity="center"

            android:text="@string/Zero"
            android:textColor="@color/DetailTextColor"
            android:textSize="20sp" />








    </LinearLayout>

</RelativeLayout>










<RelativeLayout
    android:background="@drawable/remain_background"
    android:layout_marginTop="10dp"
    android:padding="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">













    <LinearLayout
        android:id="@+id/PailRowHeaderContainer3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/glass_rounded_background"
        android:orientation="horizontal">


        <android.support.v7.widget.AppCompatTextView

            android:id="@+id/PailRowFilteredLimeStoneTitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:layout_weight="1.4"
            android:gravity="center"

            android:text="@string/FilteredLimeStone"
            android:textColor="@color/TitleTextColor"
            android:textSize="20sp" />



        <android.support.v7.widget.AppCompatTextView

            android:id="@+id/PailRowPercentageTitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:layout_weight="1.1"
            android:gravity="center"

            android:text="@string/PailPercentage"
            android:textColor="@color/TitleTextColor"
            android:textSize="20sp" />


        <android.support.v7.widget.AppCompatTextView

            android:id="@+id/PailRowTotalTitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:layout_weight="1.3"
            android:gravity="center"

            android:text="@string/Total"
            android:textColor="@color/TitleTextColor"
            android:textSize="20sp" />




    </LinearLayout>





    <LinearLayout
        android:layout_marginTop="5dp"
        android:id="@+id/PailRowDetailContainer3"
        android:layout_below="@+id/PailRowHeaderContainer3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">


        <android.support.v7.widget.AppCompatTextView

            android:id="@+id/PailRowFilteredLimeStone"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:layout_weight="1.4"
            android:gravity="center"

            android:text="@string/Zero"
            android:textColor="@color/DetailTextColor"
            android:textSize="20sp" />



        <android.support.v7.widget.AppCompatTextView

            android:id="@+id/PailRowPercentage"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:layout_weight="1.1"
            android:gravity="center"

            android:text="@string/Zero"
            android:textColor="@color/DetailTextColor"
            android:textSize="20sp" />



        <android.support.v7.widget.AppCompatTextView

            android:id="@+id/PailRowTotal"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:layout_weight="1.3"
            android:gravity="center"

            android:text="@string/Zero"
            android:textColor="@color/DetailTextColor"
            android:textSize="20sp" />




    </LinearLayout>

</RelativeLayout>

但是当我的Web服务发送此响应时,我的程序就可以工作了

[{
        "Date": "1398-01-13T00:00:00",
        "LimeStone": 2874090,
        "Shail": 1130520,
        "Silica": 0,
        "Iron": 0,
        "Gypsum": 0,
        "FilteredLimeStone": 0,
        "Percentage": "28.23",
        "Total": 4004610
    }]

0 个答案:

没有答案