如何设置 Http 标头来检索 json 对象

发布于 2024-11-03 15:29:08 字数 576 浏览 0 评论 0原文

我正在尝试创建一个返回类似请求的 httpGet ,如下所示:

http://www.myserver.com /do.json?json={"magazine":"-1"}

我似乎无法正确获取标题...... 我尝试过:

    url = "http://www.myserver.com/do.json";
    HttpGet httpGet = new HttpGet(url);
    //httpGet.setHeader("Content-type", "application/json");
    httpGet.addHeader("magazine", "-1");

    HttpResponse response = mHttpClient.execute(httpGet);
    HttpEntity entity= response.getEntity();
    is = entity.getContent();

谁能告诉我如何正确执行此操作?谢谢!

Im trying to create a httpGet that returns a similar request as :

http://www.myserver.com/do.json?json={"magazine":"-1"}

I can't seem to get the header right though....
I tried :

    url = "http://www.myserver.com/do.json";
    HttpGet httpGet = new HttpGet(url);
    //httpGet.setHeader("Content-type", "application/json");
    httpGet.addHeader("magazine", "-1");

    HttpResponse response = mHttpClient.execute(httpGet);
    HttpEntity entity= response.getEntity();
    is = entity.getContent();

Can anyone tell me how to do this properly ?? Thanks!

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

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

发布评论

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

评论(1

阳光①夏 2024-11-10 15:29:08
        HttpGet get;
        HttpParams httpParameters;
try
            {
                httpParameters = new BasicHttpParams();
                String auth = android.util.Base64.encodeToString(
                        (username + ":" + userpwd).getBytes("UTF-8"), 
                        android.util.Base64.NO_WRAP
                    );
                    HttpGet request = new HttpGet(StaticURL.uMain+resourceURI);

                    request.addHeader("Authorization", "Basic "+ auth);

                HttpConnectionParams.setSoTimeout(httpParameters, timeoutConnection);
                client = new DefaultHttpClient(httpParameters);

                response = client.execute(request);
                userAuth = EntityUtils.toString(response.getEntity());

                System.out.println("Data. in login.."+userAuth);


            }

            catch(Exception e)
            {

                System.out.println("Error.."+e);
            }

注意:外部使用Base64 api

        HttpGet get;
        HttpParams httpParameters;
try
            {
                httpParameters = new BasicHttpParams();
                String auth = android.util.Base64.encodeToString(
                        (username + ":" + userpwd).getBytes("UTF-8"), 
                        android.util.Base64.NO_WRAP
                    );
                    HttpGet request = new HttpGet(StaticURL.uMain+resourceURI);

                    request.addHeader("Authorization", "Basic "+ auth);

                HttpConnectionParams.setSoTimeout(httpParameters, timeoutConnection);
                client = new DefaultHttpClient(httpParameters);

                response = client.execute(request);
                userAuth = EntityUtils.toString(response.getEntity());

                System.out.println("Data. in login.."+userAuth);


            }

            catch(Exception e)
            {

                System.out.println("Error.."+e);
            }

note: use Base64 api externally

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