向Android http帖子添加多个标头

时间:2013-12-20 07:42:12

标签: android http-post

我需要发两个标题的帖子请求,我这样做

post.setHeader("DataServicesKey", sDataServicesKey);
        post.setHeader("SiteKey", "keySite");

这不起作用,我得到回复null但是如果我省略第二个标题,则会收到错误Missing site key我还尝试了addHeader有关如何设置多个标题的任何建议

1 个答案:

答案 0 :(得分:1)

步骤1:添加添加到JsonObject:

JSONArray mJSONArray = new JSONArray(selectedSubmit);
            JSONObject JSONSend = new JSONObject();
            JSONArray mJSONArray1 = new JSONArray(anserSubmit);

            try {
                JSONSend.put("Items", mJSONArray);
                JSONSend.put("Items1", mJSONArray1);

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

            }
            postData(
                    "url.php",
                    JSONSend);
  

步骤2:在此方法中将jsonObject发布到标题:

public void postData(String url, JSONObject obj) {
        // Create a new HttpClient and Post Header



        HttpParams myParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(myParams, 10000);
        HttpConnectionParams.setSoTimeout(myParams, 10000);

        HttpClient httpclient = new DefaultHttpClient();
        String json = obj.toString();

        try {

            HttpPost httppost = new HttpPost(url.toString());
            httppost.setHeader("Content-type", "application/json");

            StringEntity se = new StringEntity(obj.toString());
            se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                    "application/json"));
            httppost.setEntity(se);
            System.out.println("here se is " + se);
            HttpResponse response = httpclient.execute(httppost);

            String temp = EntityUtils.toString(response.getEntity());
            System.out.println(url + "sample json response" + temp);
            Log.i("tag", temp);

        } catch (ClientProtocolException e) {

        } catch (IOException e) {
        }
    }