温馨提示:本文翻译自stackoverflow.com,查看原文请点击:其他 - Request json android for Volley

其他 - 向Volley请求JSON Android

发布于 2020-03-28 23:28:16

我尝试向网站提出信息请求并返回json,我在网上遵循了一个教程,但遇到了无法通过....的问题。

带有该变量的东西一定是因为它停止了编译,但我不知道它是什么老实....

这是主要活动:

public class MainActivity extends AppCompatActivity {

    private TextView mTextViewResult;

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

        mTextViewResult = findViewById(R.id.text_view_result);
        Button buttonParse = findViewById(R.id.button_parse);

        mQueue = Volley.newRequestQueue(this);

        buttonParse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jsonParse();
            }
        });

    }

    private void jsonParse(){
        String url = "https://www.excursii-baile-felix.ro/rezervari/bot/test.php";

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        JSONArray jsonArray = null;
                        try {
                            jsonArray = response.getJSONArray("stat_rez");
                            for(int i = 0; i < jsonArray.length(); i++){

                                JSONObject res = jsonArray.getJSONObject(i);

                                String excursie = res.getString("ex");
                                int pers = res.getInt("pers");

                                mTextViewResult.append(" Excursia: " + excursie + " Persoane: " + String.valueOf(pers) + "\n\n");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }


                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        mQueue.add(request);
    }
}

而且我有以下错误:

错误:找不到符号变量mQueue

查看更多

查看更多

提问者
CoderCoder42
被浏览
23
Avadhesh Mourya 2020-01-31 17:40

您尚未声明mQueue,因此在下面声明它是正确的代码

private TextView mTextViewResult;

private RequestQueue queue;

@Override

protected void onCreate(Bundle savedInstanceState) 

{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mTextViewResult = findViewById(R.id.text_view_result);
    Button buttonParse = findViewById(R.id.button_parse);

    mQueue = Volley.newRequestQueue(this);

    buttonParse.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            jsonParse();
        }
    });

}

private void jsonParse(){
    String url = "https://www.excursii-baile-felix.ro/rezervari/bot/test.php";

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    JSONArray jsonArray = null;
                    try {
                        jsonArray = response.getJSONArray("stat_rez");
                        for(int i = 0; i < jsonArray.length(); i++){

                            JSONObject res = jsonArray.getJSONObject(i);

                            String excursie = res.getString("ex");
                            int pers = res.getInt("pers");

                            mTextViewResult.append(" Excursia: " + excursie + " Persoane: " + String.valueOf(pers) + "\n\n");
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });
    mQueue.add(request);
}