[英]Android HttpUrlConnection response is Empty and how to get the response code
我有这个代码,但响应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)
“doInBackground”的目的是在后台执行任务并在主线程中返回结果。因此,您必须从服务器返回原始结果并在“onPostExecute”中显示,而不是返回null。
@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" />
你的 connection.connect(); 用 getresponsecode 方法完成连接是没用的。 此外,请检查您的 responseCode 的值(如果它小于 200,您可能会得到 NullPointerException,因为 getErrorStream 可以为 null)
在某些情况下,您从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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.