簡體   English   中英

緩存控制:無緩存

[英]Cache-Control : no-cache

I tried to build an app in android studio which does json parsing from a URL server with ports (example: http://xxx.xxx.xxx.xxx:xxxx ). 於是我找到了asynctask、volley和okhttp等3種方法。 但問題是每次我用這些方法解析它時,總是會發生同樣的異常。

java.net.ProtocalException:意外狀態行:HTTP/1.1 Cache-Control:no-cache

這是我的代碼

 class GetJSON extends AsyncTask<Void, Void, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Please Wait");
            pDialog.setCancelable(false);
            pDialog.show();

            viewId = (TextView) findViewById(R.id.ID) ;
        }

        @Override
        protected String doInBackground(Void... voids) {
            try {
                URL url = new URL(urlWebService);
                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                StringBuilder sb = new StringBuilder();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String json;

                while ((json = bufferedReader.readLine()) != null) {
                    sb.append(json + "\n");
                }

                JSONObject data = new JSONObject(sb.toString().trim());

                return sb.toString().trim();

            } catch (final Exception e) {
                e.printStackTrace();
                resp = e.getMessage();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast toast = Toast.makeText(getApplicationContext(), resp, Toast.LENGTH_LONG);
                        toast.setGravity(Gravity.CENTER_VERTICAL, Gravity.CENTER_HORIZONTAL, 0);
                        toast.show();

                    }
                });
            }
            return null;
        }

        @Override
        protected void onPostExecute(String rawData) {
            super.onPostExecute(rawData);
            if (pDialog.isShowing())
                pDialog.dismiss();

            viewId.setText(rawData);
        }


    }

那么有什么方法可以進行這種解析嗎? 或者有什么解決辦法?

嘗試使用 postman 並使用方法GET 如果它不起作用,則可能是您的 URL 有問題。 並嘗試查看您對 URL 和 android 工作室的緩存控制。

android:usesCleartextTraffic="true"

在你的清單中使用這個

演示:

<application
        android:allowBackup="true"
        android:icon="@drawable/logo"
        android:label="@string/app_name"
        android:roundIcon="@drawable/logo"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true"
        tools:ignore="GoogleAppIndexingWarning">
        <activity android:name=".Home.Pages.viewPages.Extra.CancelAlertActivity"></activity>
        <activity android:name=".Home.HomeActivity" />
        <activity android:name=".MapsActivity" />

好吧,顯然問題不在於我使用的代碼,而在於服務器 API 的響應。 當我點擊 API 時服務器沒有響應,但現在它工作得非常好!

暫無
暫無

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

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