Java/Android:JSON中双引号处的反斜杠
我尝试向serwer发送“POST”请求。请求包括JSON。如果数据不正确,服务器将返回“ok”或my JSON(值为“message”键)。我的JSON中有奇怪的反斜杠(\sing),发送到服务器。 我得到了JSON响应:Java/Android:JSON中双引号处的反斜杠,json,backslash,Json,Backslash,我尝试向serwer发送“POST”请求。请求包括JSON。如果数据不正确,服务器将返回“ok”或my JSON(值为“message”键)。我的JSON中有奇怪的反斜杠(\sing),发送到服务器。 我得到了JSON响应: {"message":{"{\"phone_number\":\"_380661111111\",\"password\":\"112233aa\",\"military_id\":\"12345\",\"email\":\"won@mail_ru\"}":""}} 普通
{"message":{"{\"phone_number\":\"_380661111111\",\"password\":\"112233aa\",\"military_id\":\"12345\",\"email\":\"won@mail_ru\"}":""}}
普通JSON:
{"phone_number":"380666320670","password":"112233aa","military_id":"12345","email":"wovilon@mail.ru"}
完整代码:
class SendLoginData extends AsyncTask<Void, Void, Void> {
String[] key,value;
String mResultString;
SendLoginData(String[] inKey, String[] inValue ){
this.key=new String[inKey.length];
this.value=new String[inValue.length];
this.key=inKey;
this.value=inValue;
}
String resultString = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
try {// http://oasushqg.beget.tech/users
String myURL = "http://y937220i.bget.ru/users";
byte[] data = null;
InputStream is = null;
try {
URL url = new URL(myURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
OutputStream os = conn.getOutputStream();
//Create JSONObject here
JSONObject jsonParam = new JSONObject();
for (int i=0; i<this.key.length; i++) {
jsonParam.put(this.key[i], this.value[i]);
}
data=jsonParam.toString().getBytes("UTF-8");
os.write(data);
Log.d("MyLOG", "data is next: "+new String(data, "UTF-8"));
data = null;
conn.connect();
int responseCode= conn.getResponseCode();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (responseCode == 200) {
is = conn.getInputStream();
byte[] buffer = new byte[8192]; // Такого вот размера буфер
// Далее, например, вот так читаем ответ
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
baos.write(buffer, 0, bytesRead);
}
data = baos.toByteArray();
resultString = new String(data, "UTF-8");
JSONObject jsonObj=new JSONObject(resultString);
mResultString=jsonObj.getString("message");
} else {
}
} catch (MalformedURLException e) {
} catch (IOException e) {
} catch (Exception e) {
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if(resultString != null) {
Log.d("MyLOG", "postExecute run");
Log.d("MyLOG", mResultString);
}
}}
类SendLoginData扩展了异步任务{
字符串[]键,值;
字符串mResultString;
SendLoginData(字符串[]inKey,字符串[]无效){
this.key=新字符串[inKey.length];
this.value=新字符串[inValue.length];
this.key=inKey;
此值=无效;
}
字符串resultString=null;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
@凌驾
受保护的Void doInBackground(Void…参数){
试试{//http://oasushqg.beget.tech/users
字符串myURL=”http://y937220i.bget.ru/users";
字节[]数据=null;
InputStream=null;
试一试{
URL=新URL(myURL);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setRequestMethod(“POST”);
连接设置输出(真);
conn.setDoInput(真);
OutputStream os=conn.getOutputStream();
//在这里创建JSONObject
JSONObject jsonParam=新的JSONObject();
对于(int i=0;通过设置requestCode属性解决了此问题:conn.setRequestProperty(“内容类型”,“应用程序/json”);您是否解决了此问题,只需添加setRequestProperty
?我也有同样的问题,我已经设置了此问题。我的json字符串没有反斜杠)但是当请求发出时,另一端的服务器接收到带有反斜杠的json字符串。请参阅类中的其他行,它们是否与我的代码相关?如果是,您可以尝试使用免费的json测试服务器,如