如何设置Http setHeader?

时间:2016-01-28 13:36:30

标签: java android response

我使用下面的代码:

class Test extends AsyncTask < String, Void, String > {

 @Override
 protected String doInBackground(String...urls) {
  String response = "";
  for (String url: urls) {
   DefaultHttpClient client = new DefaultHttpClient();
   HttpGet httpGet = new HttpGet(url);
   try {

    HttpResponse execute = client.execute(httpGet);
    InputStream content = execute.getEntity().getContent();

    BufferedReader buffer = new BufferedReader(
     new InputStreamReader(content));
    String s = "";
    while ((s = buffer.readLine()) != null) {
     response += s;
    }

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

 @Override
 protected void onPostExecute(String result) {
  TestLogin.textView.setText(result);

 }

}

在上面的代码中应该修改哪些更改来设置标头? 我想访问我的网页jsonObjcets&#34; http://apimobile.dev2.rtbtracker.com/api/v2/users&#34;

4 个答案:

答案 0 :(得分:0)

您可以在httpGet对象上设置标题。

setHeader

之前查看client.execute(httpGet) for (var prop in obj) { var n = obj[prop]; if(n === Number(n) && n % 1 !== 0) { obj[prop] = n / 2; } }

答案 1 :(得分:0)

使用setHeader()

上的HttpGet方法
httpGet.setHeader("Header-Name", "Header Value");

在您的代码中

DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);

//Setting the header
httpGet.setHeader("Header-Name", "Header Value");

try {

   HttpResponse execute = client.execute(httpGet);

   //process

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

答案 2 :(得分:0)

httpGet.addHeader('headerName', 'headerValue'); // will work

此行将标头添加到HttpGet对象,当运行HttpClient.execute()

时,该对象将转到请求URL

答案 3 :(得分:0)

$ gnatprove --no-inlining -Pdefault.gpr -j0 --level=1 --report=all -cargs -gnatd.j
Phase 1 of 2: generation of Global contracts ...
Phase 2 of 2: flow analysis and proof ...
p.adb:12:13: medium: formal parameters "X" and "Y" might be aliased (SPARK RM 6.4.2)
p.adb:12:16: info: index check proved
p.adb:12:23: info: index check proved
p.ads:9:11: info: data dependencies proved
Summary logged in [...]/gnatprove.out