gpt4 book ai didi

java - Android http 响应出现堆栈溢出错误

转载 作者:行者123 更新时间:2023-12-02 07:14:37 26 4
gpt4 key购买 nike

我有一个后台 worker 从互联网上获取图像。

现在,当我尝试在捕获后获取图像时,出现了 Stack OverFlow。

这是我的代码

获取函数

    private InputStream fetch(String urlString) throws MalformedURLException, IOException, IllegalStateException, SocketTimeoutException { 

HttpClient httpClient = new DefaultHttpClient();

HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, TIME_OUT);
HttpConnectionParams.setSoTimeout(params, TIME_OUT);

HttpGet request = new HttpGet(urlString);
HttpResponse response = httpClient.execute(request);

return response.getEntity().getContent();
}

后台 worker

static int tryouts = 0;
/**
* Getting the bitmap from the web asynchronously
* @author orel.z
* @version 1.0
*/
class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap>
{
@Override
protected Bitmap doInBackground(String... params) {

Bitmap bitmap = null;

try
{
InputStream is = fetch(params[0]);
bitmap = BitmapFactory.decodeStream(is);
is.close();
is = null;
tryouts = 0;
}
catch(IOException exception)
{
if(tryouts < 3)
{
doInBackground(params);
tryouts++;
}
else
{
tryouts = 0;
bitmap = null;
}
}
catch (OutOfMemoryError e)
{
if(tryouts < 3)
{
System.gc();
doInBackground(params);
tryouts++;
}
else
{
tryouts = 0;
bitmap = null;
}
}

return bitmap;
}
}

这是日志

02-26 15:26:31.616: E/Crittercism(16936): java.lang.StackOverflowError
02-26 15:26:31.616: E/Crittercism(16936): at java.lang.String.substring(String.java:1436)
02-26 15:26:31.616: E/Crittercism(16936): at java.util.regex.Splitter.finishSplit(Splitter.java:105)
02-26 15:26:31.616: E/Crittercism(16936): at java.util.regex.Splitter.fastSplit(Splitter.java:75)
02-26 15:26:31.616: E/Crittercism(16936): at java.lang.String.split(String.java:1831)
02-26 15:26:31.616: E/Crittercism(16936): at java.lang.String.split(String.java:1813)
02-26 15:26:31.616: E/Crittercism(16936): at java.net.URI.isValidDomainName(URI.java:577)
02-26 15:26:31.616: E/Crittercism(16936): at java.net.URI.isValidHost(URI.java:545)
02-26 15:26:31.616: E/Crittercism(16936): at java.net.URI.parseAuthority(URI.java:491)
02-26 15:26:31.616: E/Crittercism(16936): at java.net.URI.parseURI(URI.java:409)
02-26 15:26:31.616: E/Crittercism(16936): at java.net.URI.<init>(URI.java:204)
02-26 15:26:31.616: E/Crittercism(16936): at org.apache.http.impl.conn.ProxySelectorRoutePlanner.determineProxy(ProxySelectorRoutePlanner.java:198)
02-26 15:26:31.616: E/Crittercism(16936): at org.apache.http.impl.conn.ProxySelectorRoutePlanner.determineRoute(ProxySelectorRoutePlanner.java:150)
02-26 15:26:31.616: E/Crittercism(16936): at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:596)
02-26 15:26:31.616: E/Crittercism(16936): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)
02-26 15:26:31.616: E/Crittercism(16936): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
02-26 15:26:31.616: E/Crittercism(16936): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
02-26 15:26:31.616: E/Crittercism(16936): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
02-26 15:26:31.616: E/Crittercism(16936): at com.tvincisdk.utils.ImageManager.fetch(ImageManager.java:405)
02-26 15:26:31.616: E/Crittercism(16936): at com.tvincisdk.utils.ImageManager.access$3(ImageManager.java:396)
02-26 15:26:31.616: E/Crittercism(16936): at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:319)
02-26 15:26:31.616: E/Crittercism(16936): at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936): at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936): at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936): at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936): at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936): at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936): at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936): at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936): at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936): at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936): at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936): at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936): at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936): at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936): at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936): at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936): at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936): at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936): at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936): at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936): at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936): at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936): at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936): at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936): at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936): at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936): at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936): at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936): at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager

最佳答案

这是一个更简单的逻辑。

class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap>
{
@Override
protected Bitmap doInBackground(String... params) {

Bitmap bitmap = null;

for (int tryouts=0; tryouts<3; tryouts++) {
try
{
InputStream is = fetch(params[0]);
bitmap = BitmapFactory.decodeStream(is);
is.close();
is = null;

break;
}
catch(IOException exception)
{
bitmap = null;
}
catch (OutOfMemoryError e)
{
System.gc();
bitmap = null;
}
}

return bitmap;
}
}

关于java - Android http 响应出现堆栈溢出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15090575/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com