发送数据Android Studio到PHP崩溃(&符号)

Cannot send complete string in Android Studio:

json_sales = "Client: blabla & net...."
HttpRequest req = null;
JSONArray products = null;
JSONObject json = null;
json_sales = json_sales.replace("
", " ");

HashMap<String, String> params = new HashMap<>();
params.put("data", json_sales);
params.put("tipo", "vd");

String l_resp = "";
try {
    req = new HttpRequest(server + "/importallsalses.php");
    l_resp = req.prepare(HttpRequest.Method.POST).withData(params).sendAndReadString();
} ...

file: importallsalses.php

<?php
    error_reporting(0);
    include "adodb5/adodb.inc.php";

    $data=$_POST["data"];
    $tipo=$_POST["tipo"];
    echo $data;
?>

Result - echo: "Client: blabla"

My problem is ampersand. How can I resolve this?

dp411805872
dp411805872 您应该对要发送的数据进行编码:json_sales=URLEncoder.encode(json_sales,“utf-8”);
大约 2 年之前 回复
dongshi2588
dongshi2588 你试过URL编码&?
大约 2 年之前 回复

1个回答

It looks like you are not encoding the ampersand & symbol before sending it.

Using Javas URLEncoder is likely your friend here. Changing your params.put() assignment to the below should solve it:

params.put("data", URLEncoder.encode(json_sales, "utf-8"));

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐