如何在Http Post请求中将标记作为标头和id作为参数发送?

时间:2016-08-23 11:36:22

标签: android http-post

   public static JSONObject getJSONfromURL(String url) {
    JSONObject jArray = null;
    String idTosend;
    String _accessToken = "saasfcsaas"

    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpPost httpPostRequest = new HttpPost(url);
    StringEntity se = null;     

    try {
        se = new StringEntity(_accessToken);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
   }

    httpPostRequest.addHeader("X-Auth-Token", _accessToken);

    httpPostRequest.setEntity(se);

    HttpResponse response = null;
    try {
        List<NameValuePair> nameValuePairs = new ArrayList<>();
        nameValuePairs.add(new BasicNameValuePair("Id",idTosend));

        httpPostRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        response = httpclient.execute(httpPostRequest);

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


  Here I am sending token as a header and Id as a parameter.

上面的代码正在运行但没有标题。需要显示标题的字段返回null。

请告诉我代码中的错误。

1 个答案:

答案 0 :(得分:0)

尝试以下方法:

private void addPages(ViewPager pager) {
    MyFragPagerAdapter adapter = new MyFragPagerAdapter(getSupportFragmentManager());
    Intent intent = getIntent();
    String workout = intent.getStringExtra("work");
    Bundle args = new Bundle();
    args.putString("work", work);
    String title = args.getString("work");
    switch(title) {
        case "w29w1" : {
            getSupportActionBar().setTitle("Work 29 Week 1");
            break;
        }
        case "w29w2" : {
            getSupportActionBar().setTitle("Work 29 Week 2");
            break;
        }
MondayFragment mondayFragment = new MondayFragment();
    mondayFragment.setArguments(args);

要获得最佳做法,请尝试使用Retrofit