簡體   English   中英

Android JSON Volley未解析Wordpress JSON Api

[英]Android JSON Volley not parsing Wordpress JSON Api

大家好,我使用RecyclerViewer與Volley解析JSON數據,我可以使用它。 我現在唯一的問題是從wordpress JSON API網址解析json。

如果我在服務器上創建一個json文件並訪問它就可以正常工作。 即使使用與wordpress生成的url中相同的數據

我的問題是Wordpress Json提供了不同的緩存控件或其他東西。

我更喜歡保持數據和網址的安全。 但是如果有人可以幫助我弄清楚為什么我要在下面提供的代碼無法正常工作

Json標頭Wordpress請求

AnswerHeaders

X-Firefox-Spdy  h2
cache-control   no-cache, must-revalidate, max-age=0
content-encoding    gzip
content-type    application/json; charset=UTF-8
date    Mon, 29 Oct 2018 23:26:10 GMT
expires Wed, 11 Jan 1984 05:00:00 GMT
server  nginx
x-powered-by    PleskLin

RequestHeaders

Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate, br
Accept-Language nl,en-US;q=0.7,en;q=0.3
Connection  keep-alive
Host    secret
TE  Trailers
Upgrade-Insecure-Requests   1
User-Agent  Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0

工作Json標頭

Answerheaders

Accept-Ranges   bytes
Connection  Upgrade, Keep-Alive
Content-Encoding    gzip
Content-Length  4824
Content-Type    application/json
Date    Mon, 29 Oct 2018 23:23:23 GMT
ETag    "faf7-579664b90ce89-gzip"
Keep-Alive  timeout=2, max=100
Last-Modified   Mon, 29 Oct 2018 23:19:04 GMT
Server  Apache/2
Upgrade h2,h2c
Vary    Accept-Encoding,User-Agent

RequestHeaders

Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language nl,en-US;q=0.7,en;q=0.3
Connection  keep-alive
Host    secret
If-Modified-Since   Mon, 29 Oct 2018 23:19:04 GMT
If-None-Match   "faf7-579664b90ce89-gzip"
Upgrade-Insecure-Requests   1
User-Agent  Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0

MainActivity.java

   public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private TextView mTextView;
    private String mJSONURLString = "secret<3n";
    private RequestQueue requestQueree;
    private ArrayList<TheItem> StringList;

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

        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        mTextView = (TextView) findViewById(R.id.text_test);

        mTextView.setText("");

        // Initialize a new RequestQueue instance
        StringList = new ArrayList<>();
        requestQueree = Volley.newRequestQueue(mContext);

        Button btn1 = (Button) findViewById(R.id.button_1);
        btn1.setOnClickListener(btnListener);
        loadData();
        // Initialize a new JsonObjectRequest instance
    }

    private View.OnClickListener btnListener = new View.OnClickListener() {

        public void onClick(View v) {
            //do the same stuff or use switch/case and get each button ID and do different
            loadData();
            //stuff depending on the ID
        }

    };

    public void loadData() {
        StringList.clear();
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, mJSONURLString, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        // Do something with response
                        //mTextView.setText(response.toString());

                        // Process the JSON
                        try {
                            // Get the JSON array
                            JSONArray array = response.getJSONArray("posts");

                            // Loop through the array elements
                            for (int i = 0; i < array.length(); i++) {
                                // Get current json object
                                JSONObject student = array.getJSONObject(i);

                                // Get the current student (json object) data
                                String firstName = student.getString("title");

                                Log.e("Output", firstName);
                                // Display the formatted json data in text view
                                StringList.add(new TheItem(firstName));
                            }
                            TheItem currentitem = StringList.get(0);
                            String TheTitle = currentitem.getTitle();
                            mTextView.setText(TheTitle);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // Do something when error occurred
                        String errorMessage = error.getClass().getSimpleName();
                        Log.e("error", errorMessage);
                    }
                });

        // Add JsonObjectRequest to the RequestQueue
        requestQueree.add(jsonObjectRequest);
    }
}

您必須首先實例化RequestQueue,如下所示:

final TextView mTextView = (TextView) findViewById(R.id.text);
// ...

// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";

// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        // Display the first 500 characters of the response string.
        mTextView.setText("Response is: "+ response.substring(0,500));
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        mTextView.setText("That didn't work!");
    }
});

那是W3 Total Cache,

插件正在緩存json文件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM