【问题标题】:Android HttpUrlConnection response is Empty and how to get the response codeAndroid HttpUrlConnection 响应为空以及如何获取响应代码
【发布时间】:2019-12-26 08:40:21
【问题描述】:

我有此代码,但响应 Toast 为空 谁能告诉我有什么问题? 我也不知道如何获取响应码

谢谢

 register_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                new JSONTask().execute(main_url);


            }
        });





public class JSONTask extends AsyncTask<String,String,String>{

        @Override
        protected String doInBackground(String... params) {
            HttpURLConnection connection=null;
            BufferedReader reader=null;
            StringBuilder stringBuilder = new StringBuilder();

            try {
                URL url = new URL(params[0]);
                connection=(HttpURLConnection)url.openConnection();
                connection.setRequestMethod("GET");
                InputStream stream = connection.getInputStream();
                connection.connect();


                reader = new BufferedReader(new InputStreamReader(is));


                String line = "";
                while ((line=reader.readLine()) != null){
                    stringBuilder.append(line);
                }

                return stringBuilder.toString();

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if (connection != null){
                    connection.disconnect();
                }

                if (reader != null){
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            return stringBuilder.toString();
        }
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            Toast.makeText(getApplicationContext(),result,Toast.LENGTH_SHORT).show();
        }
    }

这是错误日志

2019-12-28 10:31:23.264 2718-3484/? E/aofp: Phenotype API error. Event # bybz@2cda59b4, EventCode: 12 [CONTEXT service_id=51 ]
    aoeg: 29503
        at aofv.b(:com.google.android.gms@17786022@17.7.86 (040700-256199907):23)
        at aofp.a(:com.google.android.gms@17786022@17.7.86 (040700-256199907):92)
        at aofp.a(:com.google.android.gms@17786022@17.7.86 (040700-256199907):79)
        at aale.run(:com.google.android.gms@17786022@17.7.86 (040700-256199907):19)
        at bkdz.run(:com.google.android.gms@17786022@17.7.86 (040700-256199907):2)
        at squ.b(:com.google.android.gms@17786022@17.7.86 (040700-256199907):37)
        at squ.run(:com.google.android.gms@17786022@17.7.86 (040700-256199907):21)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at swz.run(Unknown Source:7)
        at java.lang.Thread.run(Thread.java:919)
2019-12-28 10:31:23.268 2718-3484/? E/AsyncOperation: serviceID=51, operation=GetCommittedConfigurationOperationCall
    OperationException[Status{statusCode=unknown status code: 29503, resolution=null}]
        at aofp.a(:com.google.android.gms@17786022@17.7.86 (040700-256199907):100)
        at aofp.a(:com.google.android.gms@17786022@17.7.86 (040700-256199907):79)
        at aale.run(:com.google.android.gms@17786022@17.7.86 (040700-256199907):19)
        at bkdz.run(:com.google.android.gms@17786022@17.7.86 (040700-256199907):2)
        at squ.b(:com.google.android.gms@17786022@17.7.86 (040700-256199907):37)
        at squ.run(:com.google.android.gms@17786022@17.7.86 (040700-256199907):21)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at swz.run(Unknown Source:7)
        at java.lang.Thread.run(Thread.java:919)
2019-12-28 10:31:23.268 2718-4351/? E/NetRec: [190] alcj.a: Could not retrieve server token for package com.google.android.apps.gcs
    java.util.concurrent.ExecutionException: rjk: 29503: 
        at avgu.b(:com.google.android.gms@17786022@17.7.86 (040700-256199907):3)
        at avgu.a(:com.google.android.gms@17786022@17.7.86 (040700-256199907):20)
        at alcj.a(:com.google.android.gms@17786022@17.7.86 (040700-256199907):1)
        at alcj.a(:com.google.android.gms@17786022@17.7.86 (040700-256199907):4)
        at alce.getHeaders(:com.google.android.gms@17786022@17.7.86 (040700-256199907):2)
        at com.android.volley.toolbox.HttpClientStack.performRequest(:com.google.android.gms@17786022@17.7.86 (040700-256199907):9)
        at sjg.performRequest(:com.google.android.gms@17786022@17.7.86 (040700-256199907):1)
        at byj.executeRequest(:com.google.android.gms@17786022@17.7.86 (040700-256199907):1)
        at com.android.volley.toolbox.BasicNetwork.performRequest(:com.google.android.gms@17786022@17.7.86 (040700-256199907):5)
        at sjj.performRequest(:com.google.android.gms@17786022@17.7.86 (040700-256199907):13)
        at com.android.volley.NetworkDispatcher.a(:com.google.android.gms@17786022@17.7.86 (040700-256199907):7)
        at com.android.volley.NetworkDispatcher.run(:com.google.android.gms@17786022@17.7.86 (040700-256199907):2)
     Caused by: rjk: 29503: 
        at rpj.a(:com.google.android.gms@17786022@17.7.86 (040700-256199907):4)
        at aodi.b(:com.google.android.gms@17786022@17.7.86 (040700-256199907):2)
        at aofv.a(:com.google.android.gms@17786022@17.7.86 (040700-256199907):6)
        at aale.run(:com.google.android.gms@17786022@17.7.86 (040700-256199907):30)
        at bkdz.run(:com.google.android.gms@17786022@17.7.86 (040700-256199907):2)
        at squ.b(:com.google.android.gms@17786022@17.7.86 (040700-256199907):37)
        at squ.run(:com.google.android.gms@17786022@17.7.86 (040700-256199907):21)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at swz.run(Unknown Source:7)
        at java.lang.Thread.run(Thread.java:919)

【问题讨论】:

  • Toast 为空,因为您将 nulldoInBackground() 返回到 onPostExecute() 方法。我认为存在异常或 API 的响应为空。请edit问题并在此处发布错误日志。
  • @Shashanth 已编辑

标签: java android httpurlconnection


【解决方案1】:

“doInBackground”的目的是在后台执行任务,并且 在主线程中返回结果。因此,您必须返回 null 而不是 从服务器返回原始结果并在“onPostExecute”中显示。

@Override
protected String doInBackground(String... params) {
     return null; //Remove this line and replace with below
     return stringBuilder.toString();
}

另外不要忘记添加 Internet 权限。

<uses-permission android:name="android.permission.INTERNET" />

【讨论】:

  • 完成但吐司仍然是空的!在while 之后有一个return stringBuilder.toString(); 它不工作吗?
  • 我已经在工作室检查了你的代码,通过更改返回语句它工作正常。我相信你没有添加互联网权限。确保添加“ 清单中的权限"
  • 错误日志上写着java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
  • 捕获异常并检查导致错误的异常
【解决方案2】:

你的connection.connect();没用的连接是用getresponsecode方法完成的。此外检查您的 responseCode 的值(如果它小于 200,您可能会得到 NullPointerException,因为 getErrorStream 可以为空)

【讨论】:

    【解决方案3】:

    在某些情况下,您从 doInBackground 返回 null。而不是null 返回stringBuilder.toString()。检查以下:

        @Override
        protected String doInBackground(String... params) {
            HttpURLConnection connection=null;
            BufferedReader reader=null;
            StringBuilder stringBuilder = new StringBuilder(); // initialize here
    
            ... 
            //Other stuff here
    
            return stringBuilder.toString();
        }
    

    【讨论】:

      猜你喜欢
      • 2012-02-09
      • 1970-01-01
      • 1970-01-01
      • 2016-12-14
      • 1970-01-01
      • 2012-07-20
      • 1970-01-01
      • 1970-01-01
      • 2020-09-12
      相关资源
      最近更新 更多