【发布时间】:2017-05-16 15:00:30
【问题描述】:
我似乎有以下 asyncTask 的内存泄漏,可能是什么原因?
/**
* Async task class to get json by making HTTP call
* */
public class PostLocation extends AsyncTask<String, Void, String>
{
@Override
protected void onPreExecute()
{
}
@Override
protected String doInBackground(String... params)
{
try
{
String response = "";
URL url = new URL(BASE_URL + "receiveLocation.php");
myConnection = (HttpURLConnection) url.openConnection();
myConnection.setRequestMethod("POST");
myConnection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(myConnection.getOutputStream());
wr.writeBytes(params[0]);
int responseCode = myConnection.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK)
{
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(myConnection.getInputStream()));
while ((line = br.readLine()) != null)
{
response += line;
}
}
else
{
return response = "";
}
return response;
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return "false";
}
}
@Override
protected void onPostExecute(String result)
{
if(result.equals("success"))
{
myConnection.disconnect();
}
}
}
这只是将一些数据发布到我的服务器。什么可能导致内存泄漏,注意这个任务是在后台使用 goAsync 从广播接收器调用的
【问题讨论】:
-
添加 logcat 将提供有关问题的更多信息,请添加它。
-
这个类是放在单独的.java文件中还是嵌套类?
-
@Kelevandos 它是我的广播接收器内部的一个内部类
-
请看答案
标签: android memory-leaks android-asynctask