Android HttpsURLConnection 与 HttpClient,两者都会出现问题

发布于 2024-12-15 02:08:37 字数 1521 浏览 3 评论 0 原文

我读到 HttpsURLConnection 在 2.3 之前就有 bug,并且我有它有 bug 的第一手经验。

我尝试的替代方案是 HttpClient,但是它给我带来了问题,因为它返回了我这个:

<?xml version="1.0" encoding="iso-8859-1"?>

当我显然应该返回 JSON 时。

HttpsURLConnection 可以正常返回 JSON,但在处理许多请求时会出现一些不一致的结果。 (通过返回 JSON,我的意思是响应应该包含 JSON)

知道为什么 HttpClient 返回一些 xml 标签而不是 JSON(应该返回什么)吗?

编辑 - 代码

HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);

    List<NameValuePair> urlParams = new ArrayList<NameValuePair>();
        urlParams.add(new BasicNameValuePair("&platform=","Android"));
        urlParams.add(new BasicNameValuePair("&action=","signin"));
        urlParams.add(new BasicNameValuePair("&user",username));
        urlParams.add(new BasicNameValuePair("&pass",password));



    try {

        post.setEntity(new UrlEncodedFormEntity(urlParams, HTTP.UTF_8));
        HttpResponse resp = client.execute(post);
        BufferedReader rd = new BufferedReader(new InputStreamReader(
                resp.getEntity().getContent()));

        String line = "";
        JSONArray ja = new JSONArray();
        while ((line = rd.readLine()) != null) {
            Log.i("------>", line);
            ja = new JSONArray(line); //breaks here because it returns xml instead of JSON

        }
        rd.close();
        return ja;

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

这两个有更好的替代品吗?

I read that HttpsURLConnection is buggy before 2.3 and I have firsthand experience with it being buggy.

The alternate that I tried is HttpClient, however it gives me problems in the sense that it returns me this:

<?xml version="1.0" encoding="iso-8859-1"?>

when I'm clearly supposed to get back JSON.

HttpsURLConnection returns JSON back fine but has some inconsistent results when dealing with many requests. (By return JSON, I mean that the response is supposed to contain JSON)

Any idea why HttpClient gives back some xml tag instead of JSON (what should be returned) ?

EDIT -- CODE

HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);

    List<NameValuePair> urlParams = new ArrayList<NameValuePair>();
        urlParams.add(new BasicNameValuePair("&platform=","Android"));
        urlParams.add(new BasicNameValuePair("&action=","signin"));
        urlParams.add(new BasicNameValuePair("&user",username));
        urlParams.add(new BasicNameValuePair("&pass",password));



    try {

        post.setEntity(new UrlEncodedFormEntity(urlParams, HTTP.UTF_8));
        HttpResponse resp = client.execute(post);
        BufferedReader rd = new BufferedReader(new InputStreamReader(
                resp.getEntity().getContent()));

        String line = "";
        JSONArray ja = new JSONArray();
        while ((line = rd.readLine()) != null) {
            Log.i("------>", line);
            ja = new JSONArray(line); //breaks here because it returns xml instead of JSON

        }
        rd.close();
        return ja;

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

Are there any better alternates to these two?

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

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

发布评论

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

评论(3

旧伤还要旧人安 2024-12-22 02:08:37

这看起来像是来自服务器的错误页面响应。您可能会收到 404 错误或许多其他 http 错误。这只是您从服务器返回的 html 文件的开头。您确定该网址是有效的网址并且会接受来自移动设备的连接而不进行重定向吗?

This looks like a response from the server of an error page. You might be getting a 404 error or many other http errors. This is just the beginning of the html file you are getting back from the server. Are you sure the url is a valid url and will accept a connection from mobile without redirecting?

纸伞微斜 2024-12-22 02:08:37

您应该从 BasicNameValuePair 键 中删除 "&" 字符。

BasicNameValuePair 自动在每个参数中附加 "&",因此无需添加 "&" 字符

您必须编写如下

urlParams.add(new BasicNameValuePair("platform","Android"));

You should remove the "&" character from the BasicNameValuePair key.

BasicNameValuePair autometically append "&" in each parameter so no need to add "&" character

You have to write as below

urlParams.add(new BasicNameValuePair("platform","Android"));
错々过的事 2024-12-22 02:08:37

而不是:

 List<NameValuePair> urlParams = new ArrayList<NameValuePair>();
        urlParams.add(new BasicNameValuePair("&platform=","Android"));
        urlParams.add(new BasicNameValuePair("&action=","signin"));
        urlParams.add(new BasicNameValuePair("&user",username));
        urlParams.add(new BasicNameValuePair("&pass",password));

尝试这个:

 List<NameValuePair> urlParams = new ArrayList<NameValuePair>();
        urlParams.add(new BasicNameValuePair("platform","Android"));
        urlParams.add(new BasicNameValuePair("action","signin"));
        urlParams.add(new BasicNameValuePair("user",username));
        urlParams.add(new BasicNameValuePair("pass",password));

更新1:

你到底想在这里实现什么:

 String line = "";
        JSONArray ja = new JSONArray();
        while ((line = rd.readLine()) != null) {
            Log.i("------>", line);
            ja = new JSONArray(line); //breaks here because it returns xml instead of JSON

        }

它显然给出了错误,因为你试图从XML响应字符串创建JSONArray,intsead检查这个public JSONArray (String json) 函数并实现它。

Instead of:

 List<NameValuePair> urlParams = new ArrayList<NameValuePair>();
        urlParams.add(new BasicNameValuePair("&platform=","Android"));
        urlParams.add(new BasicNameValuePair("&action=","signin"));
        urlParams.add(new BasicNameValuePair("&user",username));
        urlParams.add(new BasicNameValuePair("&pass",password));

Try this:

 List<NameValuePair> urlParams = new ArrayList<NameValuePair>();
        urlParams.add(new BasicNameValuePair("platform","Android"));
        urlParams.add(new BasicNameValuePair("action","signin"));
        urlParams.add(new BasicNameValuePair("user",username));
        urlParams.add(new BasicNameValuePair("pass",password));

Update 1:

What exactly are you trying to implement here:

 String line = "";
        JSONArray ja = new JSONArray();
        while ((line = rd.readLine()) != null) {
            Log.i("------>", line);
            ja = new JSONArray(line); //breaks here because it returns xml instead of JSON

        }

It obvious gives error because you are trying to create JSONArray from the XML response string, intsead check this public JSONArray (String json) function and implement it.

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