Java / Android:双引号位置的JSON中的反斜杠

时间:2017-02-14 15:37:33

标签: json backslash

我尝试向serwer发送“POST”请求。请求包括JSON。如果数据不正确,服务器返回“ok”或我的JSON(值为“message”键)。我在我的JSON中发送奇怪的反斜杠(\ sing),发送到服务器。  我收到JSON回复:

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

    }

}}

0 个答案:

没有答案