http 响应被切断时出现的字符串? - Java,平台Android 3.0

发布于 2024-11-18 16:46:17 字数 3556 浏览 1 评论 0原文

我遇到了一个非常奇怪的问题,我完全不知道从哪里开始。

我正在向服务器发送一个 http 请求并获得一个简单的字符串作为响应。这在我的智能手机应用程序中运行良好;它甚至在我的浏览器中运行良好。然而,虽然我以为我只是复制并粘贴智能手机代码,但它不再适用于我的平板电脑 (Android 3.0.1) 版本的应用程序。

我已经检查了调试器,旧版本得到了一个长度为 2958 个字符的字符串。不过,新版本只获取长度为 1334 的字符串。我记录了新版本的 URL,将其放入浏览器中,再次得到一串 2985 个字符的字符串。

我真的找不到我的代码有任何重大差异(请参见下文)。另外,我不敢相信 Android 中发生了一些限制字符串长度的变化?!

那么有人有想法吗?


原始智能手机代码:

if (CheckInternet())
{
    myURL = new URL(params[0]);

    httpClient = AndroidHttpClient.newInstance("android");

    if (rtype == RequestType.GET)
    {
        httpRequest = new HttpGet(myURL.toExternalForm());
    }
    else
    {
        httpRequest = new HttpPost(myURL.toExternalForm());

        HttpEntity myEntity = new StringEntity(message, "UTF-8");
        ((HttpPost) httpRequest).setEntity(myEntity);
    }


    HttpParams httpParams = new BasicHttpParams();
    HttpProtocolParams.setHttpElementCharset(httpParams, "UTF-8");
    HttpProtocolParams.setContentCharset(httpParams, "UTF-8");
    HttpConnectionParams.setConnectionTimeout(httpParams, timeout);
    HttpConnectionParams.setSoTimeout(httpParams, timeout);
    httpRequest.setParams(httpParams);

    response = httpClient.execute(httpRequest);

    final int statusCode = response.getStatusLine().getStatusCode();

    if (statusCode == 300 || statusCode >= 305)
    {
        errorMessage = getStatusCodeMessage(statusCode, act);
    }
    else
    {
        HttpEntity entity = response.getEntity();

        if (entity != null)
        {

            InputStream instream = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(instream, "UTF-8"));
            StringBuilder sb = new StringBuilder();

            String line = null;
            while ((line = reader.readLine()) != null)
                sb.append(line);

            result = sb.toString();

        }
    }
}

新平板电脑版本中的代码:

if (CheckInternet())
{
    if (isCancelled()) return null; //that's for an AsyncTask

    URL myURL = new URL(params[0]);
    httpClient = AndroidHttpClient.newInstance("android");

    if (isCancelled()) return null;

    if (params[1] == null)
    {
        httpRequest = new HttpGet(myURL.toExternalForm());
    }
    else
    {
        httpRequest = new HttpPost(myURL.toExternalForm());

        HttpEntity myEntity = new StringEntity(params[1], "UTF-8");
        ((HttpPost) httpRequest).setEntity(myEntity);
    }

    httpRequest.setParams(httpParams);

    if (isCancelled()) return null;

    HttpResponse response = httpClient.execute(httpRequest);
    httpClient.close();

    if (isCancelled()) return null;

    final int statusCode = response.getStatusLine().getStatusCode();

    if (statusCode == 300 || statusCode >= 305)
    {
        error = HttpHelper.getStatusCodeMessage(statusCode, getActivity());
    }
    else
    {
        if (isCancelled()) return null;

        HttpEntity entity = response.getEntity();

        if (entity != null)
        {

            InputStream instream = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(instream, "UTF-8"));
            StringBuilder sb = new StringBuilder();

            String line = null;
            while ((line = reader.readLine()) != null)
                sb.append(line);

            String test = sb.toString(); //that was for debugging the string
            return test;

        }
    }
}

两个请求都在 AsyncTask 中运行。

亲切的问候, 海蜇

I have run into a very strange problem and I don't have the slightest idea where to start.

I am sending a http request to a server and get a simple string as response. This worked fine in my smartphone app; it even works fine in my browser. However, while I thought I'd simply copy-and-pasted the smartphone code, it doesn't work for my tablet (Android 3.0.1) version of the app anymore.

I have checked with the debugger and the old version gets a string with a length of 2958 characters. The new version only gets a string of the length 1334, though. I've logged the URL of the new version, put it into my browser and got a string of 2985 characters again.

I really can't find any major difference in my code (please see below). Also, I can't believe there was some change in Android that would limit string length?!

So does anybody have an idea?


Original Smartphone code:

if (CheckInternet())
{
    myURL = new URL(params[0]);

    httpClient = AndroidHttpClient.newInstance("android");

    if (rtype == RequestType.GET)
    {
        httpRequest = new HttpGet(myURL.toExternalForm());
    }
    else
    {
        httpRequest = new HttpPost(myURL.toExternalForm());

        HttpEntity myEntity = new StringEntity(message, "UTF-8");
        ((HttpPost) httpRequest).setEntity(myEntity);
    }


    HttpParams httpParams = new BasicHttpParams();
    HttpProtocolParams.setHttpElementCharset(httpParams, "UTF-8");
    HttpProtocolParams.setContentCharset(httpParams, "UTF-8");
    HttpConnectionParams.setConnectionTimeout(httpParams, timeout);
    HttpConnectionParams.setSoTimeout(httpParams, timeout);
    httpRequest.setParams(httpParams);

    response = httpClient.execute(httpRequest);

    final int statusCode = response.getStatusLine().getStatusCode();

    if (statusCode == 300 || statusCode >= 305)
    {
        errorMessage = getStatusCodeMessage(statusCode, act);
    }
    else
    {
        HttpEntity entity = response.getEntity();

        if (entity != null)
        {

            InputStream instream = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(instream, "UTF-8"));
            StringBuilder sb = new StringBuilder();

            String line = null;
            while ((line = reader.readLine()) != null)
                sb.append(line);

            result = sb.toString();

        }
    }
}

Code in the new Tablet version:

if (CheckInternet())
{
    if (isCancelled()) return null; //that's for an AsyncTask

    URL myURL = new URL(params[0]);
    httpClient = AndroidHttpClient.newInstance("android");

    if (isCancelled()) return null;

    if (params[1] == null)
    {
        httpRequest = new HttpGet(myURL.toExternalForm());
    }
    else
    {
        httpRequest = new HttpPost(myURL.toExternalForm());

        HttpEntity myEntity = new StringEntity(params[1], "UTF-8");
        ((HttpPost) httpRequest).setEntity(myEntity);
    }

    httpRequest.setParams(httpParams);

    if (isCancelled()) return null;

    HttpResponse response = httpClient.execute(httpRequest);
    httpClient.close();

    if (isCancelled()) return null;

    final int statusCode = response.getStatusLine().getStatusCode();

    if (statusCode == 300 || statusCode >= 305)
    {
        error = HttpHelper.getStatusCodeMessage(statusCode, getActivity());
    }
    else
    {
        if (isCancelled()) return null;

        HttpEntity entity = response.getEntity();

        if (entity != null)
        {

            InputStream instream = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(instream, "UTF-8"));
            StringBuilder sb = new StringBuilder();

            String line = null;
            while ((line = reader.readLine()) != null)
                sb.append(line);

            String test = sb.toString(); //that was for debugging the string
            return test;

        }
    }
}

Both requests are running in an AsyncTask.

Kind regards,
jellyfish

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青芜 2024-11-25 16:46:17

我不确定这就是原因,但它看起来很可疑 -

HttpResponse response = httpClient.execute(httpRequest);
httpClient.close(); // <--

我会等到消耗 HttpEntity 之后再关闭客户端。

I'm not sure this is the cause, but it looks suspicious -

HttpResponse response = httpClient.execute(httpRequest);
httpClient.close(); // <--

I'd wait until after consuming the HttpEntity before closing the client.

无尽的现实 2024-11-25 16:46:17

我自己也是新手,所以如果我听起来像个白痴,请原谅我。
我在这篇文章中发现了一个有趣的点:

http:// android-developers.blogspot.com/2010/12/new-gingerbread-api-strictmode.html

它说“你不应该在主线程上执行网络请求。事实上,在即将到来的Honeycomb 版本 我们已将主线程上的网络请求设置为致命错误除非您的应用面向 Honeycomb 之前的 API 版本

您是否正在运行请求在单独的 ASyncThread 中?我无法通过查看代码来判断。我自己做这件事真是太开心了。如果您有任何想法,请告诉我,因为我很想看看您是如何做到的。

真挚地,
安德鲁

I am new to this myself, so please forgive me if I sound like an idiot.
I found an interesting point in this article:

http://android-developers.blogspot.com/2010/12/new-gingerbread-api-strictmode.html

It said "you should never do network requests on your main thread. In fact, in the upcoming Honeycomb release we’ve made network requests on the main thread a fatal error, unless your app is targeting an API version before Honeycomb"

Are you running your request in a separate ASyncThread? I cant tell by looking at the code. I am having a doozie of a time doing this myself. Please let me know if you come up with anything, as I would LOVE to see how you did it.

Sincerely,
Andrew

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文