Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中将标题传递给WebView_Android_Android Webview_Postdata - Fatal编程技术网

如何在Android中将标题传递给WebView

如何在Android中将标题传递给WebView,android,android-webview,postdata,Android,Android Webview,Postdata,我使用postrl()将post数据作为Json传递给Android中的WebView。现在,我想同时传递一个标题“Content-Type:application/json”。如何实现它?这可能会对你有所帮助:) 我相信这将有助于: 看起来你不能用postrl()发送标题,只能用loadUrl()方法发送。可以用标题发布数据我在我的项目中已经做过了 我给你发我的代码 HttpParams par = new BasicHttpParams();

我使用postrl()将post数据作为Json传递给Android中的WebView。现在,我想同时传递一个标题“Content-Type:application/json”。如何实现它?

这可能会对你有所帮助:)

我相信这将有助于:
看起来你不能用postrl()发送标题,只能用loadUrl()方法发送。

可以用标题发布数据我在我的项目中已经做过了 我给你发我的代码

            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");

请看我的答案,这将帮助uThanks找到答案。那么httpclient将返回什么样的响应呢?它可以设置为WebView,对吗?哪个web返回给我们的数据是响应?它是web调用@VivekMy的post方法问题是它将返回的响应是html格式吗?WebVew的loadUrl()方法支持传递标题。没有办法用postrl()方法传递头吗?因为使用上述方法,它将首先建立httpconnection并将数据传递给服务器,所以响应将通过loadData()方法设置为WebView。
            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");