如何设置一个HTTP头以检索一个JSON对象 [英] How to set a Http header to retrive a json Object

查看:137
本文介绍了如何设置一个HTTP头以检索一个JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去创建一个HTTPGET返回一个类似的要求为:

http://www.myserver.com/do.json?json= {周刊: - 1}

我似乎无法得到正确的头虽然.... 我想:

  URL =htt​​p://www.myserver.com/do.json;
    HTTPGET HTTPGET =新HTTPGET(URL);
    //httpGet.setHeader("Content-type,应用/ JSON);
    httpGet.addHeader(杂志, -  1);

    HTT presponse响应= mHttpClient.execute(HTTPGET);
    HttpEntity实体= response.getEntity();
    是= entity.getContent();
 

谁能告诉我如何正确地做到这一点?谢谢!

解决方案

  HTTPGET搞定;
        的HttpParams httpParameters;
尝试
            {
                httpParameters =新BasicHttpParams();
                字符串AUTH = android.util.Base64.en codeToString(
                        (用户名+:+ userpwd).getBytes(UTF-8),
                        android.util.Base64.NO_WRAP
                    );
                    HTTPGET请求=新HTTPGET(StaticURL.uMain + resourceURI);

                    request.addHeader(授权,基本+ AUTH);

                HttpConnectionParams.setSoTimeout(httpParameters,timeoutConnection);
                客户端=新DefaultHttpClient(httpParameters);

                响应= client.execute(要求);
                USERAUTH = EntityUtils.toString(response.getEntity());

                的System.out.println(数据在登录......。+ USERAUTH);


            }

            赶上(例外五)
            {

                的System.out.println(错误..+ E);
            }
 

请注意:使用的Base64 API外部

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!

解决方案

        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

这篇关于如何设置一个HTTP头以检索一个JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆