2

I am passing post data as Json using postUrl() to WebView in Android. Now I want to pass a header "Content-Type: application/json" also along with that. How to achieve it?

Vivek
  • 1,823
  • 1
  • 19
  • 39

3 Answers3

0

may this will help u :)

       HttpPost httpPost = new HttpPost(url_src);
HttpParams httpParameters = new BasicHttpParams();
httpclient.setParams(httpParameters);
StringEntity se = new StringEntity(str,"UTF-8");
    se.setContentType("application/json");
httpPost.setEntity(se); 

     httpPost.setHeader("userId", ""+userId);//userID
     httpPost.setHeader("machineId", machineId);//deviceUserMachineID

     response = httpclient.execute(httpPost);
Bhanu Sharma
  • 5,135
  • 2
  • 24
  • 49
0

I believe this will help: link Doesn't look like you can actually send headers with postUrl(), only with loadUrl() method.

Community
  • 1
  • 1
ManiacalG
  • 221
  • 1
  • 2
  • 7
0

it possible to post data with header i have done it in my project I am posting you my code ...

            HttpParams par = new BasicHttpParams();
            //par.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
            int timeoutConnection = 30000;
            HttpConnectionParams.setConnectionTimeout(par, timeoutConnection);
            int timeoutSocket = 10000;
            HttpConnectionParams.setSoTimeout(par, timeoutSocket);

            DefaultHttpClient httpClient = new DefaultHttpClient(par);
         //   httpClient.setParams(par);

            HttpPost httpPost = new HttpPost(url);

                 httpPost = new HttpPost(url);
                 if(postMessage==null)
                     throw new IllegalArgumentException("please send post data as well");
                byte[] data =postMessage.toString().getBytes("UTF-8");

                String base64 = Base64.encodeToString(data, Base64.DEFAULT);
                System.out.println("Base64: "+base64);

                // httpPost.setEntity(new UrlEncodedFormEntity(params));
                  httpPost.setEntity(new StringEntity(base64));

            httpPost.setHeader("token", app.getToken());


           httpResponse = httpClient.execute(httpPost);
           HttpEntity httpEntity = httpResponse.getEntity();


             is = httpEntity.getContent();
               System.out.println("httpEntity.getContent():"+ is );
             BufferedReader reader = new BufferedReader(new InputStreamReader(
              is, "iso-8859-1"));

             StringBuilder sb = new StringBuilder();
             String line = null;
             while ((line = reader.readLine()) != null) {
                 sb.append(line + "\n");
             }
             is.close();
             data = sb.toString();
              //remember httpconnection should be in background thread 
              // if you use asynchtask then upper code should be in doinbackground method 
              //and in onpostexecution do data load in webview

             webview.loadData(data, "text/html","UTF-8");
AndroidLad
  • 687
  • 7
  • 14