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

时间:2011-04-28 06:46:34

标签: android json

我试图创建一个返回类似请求的httpGet:

http://www.myserver.com/do.json?json= { “杂志”: “ - 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();

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

1 个答案:

答案 0 :(得分:8)

        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