调试后,我的代码给了我异常“空对象指针引用”,发现响应变量(response = httpClient.execute(httpGet,localContext);)为空。 我认为,我的错误是使用HTTP客户端。我如何将其转换为HTTPS,因为它是必需的,但是我不确定。
class MyTask extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String ... url) {
String url2=url[0];
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
//String url2=url[0];
HttpGet httpGet = new HttpGet(url2);
HttpResponse response = null;
try {
response = httpClient.execute(httpGet, localContext);
} catch (IOException e) {
e.printStackTrace();
}
String result = "";
BufferedReader reader = null;
try {
reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent()
)
);
} catch (IOException e) {
e.printStackTrace();
}
String line = null;
while (true) {
try {
if (!((line = reader.readLine()) != null)) break;
} catch (IOException e) {
e.printStackTrace();
}
result += line + "\n";
}
String FILENAME = "file";
FileOutputStream fos = null;
try {
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
fos.write(result.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
如果我错了,请告诉我。预先谢谢你