Sourabh Saldi
Sourabh Saldi

Reputation: 3587

adding multiple headers to Android http post

I need to make a post request with two headers, I do this as

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

This does not work, I get response null however if i omitt second header, I get error Missing site key I have also tried addHeader any suggestions on how to set multiple headers

Upvotes: 0

Views: 1802

Answers (1)

KOTIOS
KOTIOS

Reputation: 11194

Step 1 : Add add to 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);

Step 2: post that jsonObject to header here in this method (based on this answer by Sachin Gurnani):

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) {
    }
}

Upvotes: 1

Related Questions