【问题标题】:Send json in posturl webview在 posturl webview 中发送 json
【发布时间】:2016-09-19 08:42:29
【问题描述】:

我如何在WebView 中发送JSON 发布数据以使用原始数据调用网络服务?

这是我的代码,

fb.loadData(base64, "text/html; charset=UTF-8", "base64");

【问题讨论】:

  • 使用WebView调用webservice有什么意义?

标签: java android android-studio


【解决方案1】:

假设这是你的 json: 1.

ReqBody : [{
                                'LoginId':'LoginId',                      
                                'pass' :'pass'                      
                            }
                        ]

您的 pojo 课程将是:

public class ExamplePojo{
        @SerializedName("LoginId")
        private String LoginId;
        @SerializedName("pass")
        private String Password;

        //getter setter method  
          }

2.使用Gson将指定对象序列化成其等效的Json表示。

 public static String getJsonString(Object obj) throws JSONException {
        Gson gson = new Gson();
        if (obj != null) {
            String json = gson.toJson(obj);
            JSONObject jsonObject = new JSONObject(json);


            return jsonObject.toString();
        } else
            return "";
    }

3.Post this data(Return by getJsonString() say dataToPost ) as per your requirement

4.假设您必须以键值对的形式发送数据,然后添加以下内容:

NameValuePair dataToSend = new NameValuePair("key", dataToPost);
 postData = getQuery(dataToSend);



private String getQuery(NameValuePair params) throws UnsupportedEncodingException {
        StringBuilder result = new StringBuilder();

        result.append(URLEncoder.encode(params.getKey(), "UTF-8"));
        result.append("=");
        result.append(URLEncoder.encode(params.getValue(), "UTF-8"));
//        }

        return result.toString();
    }

try {
       mWebView.postUrl(URL, postData.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }


    OR



URL url;
            String response = "";
            try {
                url = new URL(URL);

                HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
                conn.setReadTimeout(TIMEOUT);
                conn.setConnectTimeout(TIMEOUT);
                conn.setRequestMethod("POST");
                conn.setChunkedStreamingMode(0);
                conn.setDoInput(true);
                conn.setDoOutput(true);


                OutputStream os = conn.getOutputStream();
                BufferedWriter writer = new BufferedWriter(
                        new OutputStreamWriter(os, "UTF-8"));
                writer.write(dataToSend);

                writer.flush();
                writer.close();
                os.close();
                int responseCode = conn.getResponseCode();

                if (responseCode == HttpsURLConnection.HTTP_OK) {
                    String line;
                    BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                    while ((line = br.readLine()) != null) {
                        response += line;
                    }
                } else {
                    response = "";

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

            return response;

响应将是 html 页面,您可以在 webview 上发布如下: mWebView.loadData(response, "text/html", "utf-8");

【讨论】:

  • 我使用 webview.posturl() 然后在打开 webview 后给出 Disallowed key charachters
【解决方案2】:

试试

    webView.postUrl("url", data.getBytes("UTF-8"));

【讨论】:

  • 请编辑更多信息。不建议使用纯代码和“试试这个”的答案,因为它们不包含可搜索的内容,也没有解释为什么有人应该“试试这个”。
  • 好的,我会尽量解释答案,谢谢你的好建议。
【解决方案3】:

如果你想将jsonObject数据作为api body传递给webview,你可以试试这个->

HashMap map = new Gson().fromJson(jsonObject.toString(), HashMap.class);

字符串 postdata1 = "";

int i = 1;

for (Map.Entry entry : map.entrySet()) { 后数据1 = postdata1.concat(entry.getKey().concat("=").concat(URLEncoder.encode(entry.getValue()+"", "UTF-8")).concat(i == map.size() ? "" : "&")); 我++; }

webView.postUrl(url, postdata1 .getBytes());

直接使用这样的键值

"KEY1="+ URLEncoder.encode(postData.getAmount().toString(), "UTF-8")+

                "&KEY2="+URLEncoder.encode(postData.getBackref(), "UTF-8") +

                "&KEY3="+URLEncoder.encode(postData.getCountry(), "UTF-8") +

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-02
    • 2013-11-20
    • 2011-12-11
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    相关资源
    最近更新 更多