繁体   English   中英

无法使用 asynchTask 从服务器获得响应

[英]Unable to get response from the server using asynchTask

我正在尝试创建登录,并且用户信息位于远程服务器上,但是当我运行应用程序以获取令牌时,我不断收到W/System.err: java.io.FileNotFoundException:类型的错误W/System.err: java.io.FileNotFoundException: https://uat .elma.bz/revenueapi/token ".Api 正在通过 Postman 工作,不确定可能是什么问题,请您提出建议。

private class userlogin extends AsyncTask<String, String, String> {
        @Override
        public void onPreExecute() {
          /*  progressDialog = new ProgressDialog(userLogin.this);
            progressDialog.setMessage("please wait as you get connected to the internet");
            progressDialog.setIndeterminate(true);
            progressDialog.setCancelable(false);
            progressDialog.show();*/
        }

        @Override
        public String doInBackground(String... para) {
            String response = "";

            Map<String, String> params = new LinkedHashMap<>();
            URL url = null;
            try {
                url = new URL(webconfigs.LOGIN_URL);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            ;
            params.put("grant_type", webconfigs.GRANT_TYPE);
            params.put("username", para[0]);
            params.put("Content-Type", webconfigs.CONTENT_TYPE);
            params.put("password", para[1]);
            StringBuilder postData = new StringBuilder();
            for (Map.Entry<String, String> param : params.entrySet()) {
                try {
                    if (postData.length() != 0) postData.append('&');
                    try {
                        postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    postData.append('=');
                    postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));

                } catch (Exception e) {
                }
            }
            try {
                String urlparameters = postData.toString();
                URLConnection conn = url.openConnection();
                conn.setRequestProperty("Method", "POST");
                conn.setDoOutput(true);
                OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
                writer.write(urlparameters);
                writer.flush();

                String line = "";
                BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                //just to show the result
                Log.e("READER", response);
                while (String.valueOf(line = reader.readLine()) != null) {
                    response += line;
                }
                writer.close();
                reader.close();
                return response;

            } catch (Exception e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            Log.e(">>>>LOGINSERVERRESPONSE", ">>>>>" + result);
            //progressDialog.dismiss();
            try {
                JSONObject results = new JSONObject(result);
                String token = results.getString("access_token");
                if (token != null) {
                    String accessToken = results.getString("access_token");
                    String tokenType = results.getString("token_type");
                    String dateIssued = results.getString(".issued");
                    String expiryDate = results.getString(".expires_in");
                    startActivity(new Intent(getApplicationContext(), Homedashboard.class));
                }

            } catch (Exception e) {
            }
        }

    }

您能否在 AndroidManifest.xml 中添加此部分( android:usesCleartextTraffic="true" )? 像这样。

<manifest ...>


  <application
              ...
              android:usesCleartextTraffic="true">

  </application>

</manifest>

暂无
暂无

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

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