简体   繁体   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 ). 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 ). So I found 3 methods such as asynctask, volley and okhttp.于是我找到了asynctask、volley和okhttp等3种方法。 But the problem is everytime I parse it with these methods, the same exception always occurred.但问题是每次我用这些方法解析它时,总是会发生同样的异常。

java.net.ProtocalException: Unexpected status line: HTTP/1.1 Cache-Control:no-cache java.net.ProtocalException:意外状态行:HTTP/1.1 Cache-Control:no-cache

Here is my code这是我的代码

 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);
        }


    }

So is there any method to do this parsing?那么有什么方法可以进行这种解析吗? Or any solution?或者有什么解决办法?

Try to use postman and use the method GET .尝试使用 postman 并使用方法GET If it doesnt work, probably there is something wrong with your URL.如果它不起作用,则可能是您的 URL 有问题。 And also try to see your cache control of both your URL and android studio.并尝试查看您对 URL 和 android 工作室的缓存控制。

android:usesCleartextTraffic="true" android:usesCleartextTraffic="true"

use this In Your Manifest在你的清单中使用这个

DEMO:演示:

<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" />

Okay so apparently the problem was not with the code I am using, but the server API's responses.好吧,显然问题不在于我使用的代码,而在于服务器 API 的响应。 The server didn't response when I hit the API but now it's working perfectly fine!当我点击 API 时服务器没有响应,但现在它工作得非常好!

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

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