当使用codeName发送post请求时,Json String为空

时间:2017-09-30 16:14:38

标签: java json codenameone

我正在尝试使用#!/bin/bash dirfile='.trash' verbose= function helpfunction() { echo "=============== HELP COMMAND =============" echo "" echo "-h | --help" echo "-i | --interactive" echo "-v | --verbose" echo "-r | --recursive" echo "" } if [ $# -eq 0 ]; then echo "no commands" exit 1 fi option="${1}" case ${option} in -h | --help) helpfunction exit ;; #interactive -i or --interactive -i) FILE="${*}" echo "File name is $FILE" echo -n "Do you want to remove this file (y/n)? " read answer if echo "$answer" | grep -iq "^y" ;then mv $FILE $dirfile fi ;; #verbose -v or --verbose -v) FILE="${*}" if [ -f "$FILE" ]; then -v $FILE fi ;; #recursive -r or --recursive -r) FILE="${*}" if [ -d "${*}" ]; then rm -r "$FILE" fi ;; #unremove -u or --unremove -u) FILE="${*}" for file in $dirfile*; do if [[ -f $file ]]; then echo "file found" else echo "File not found" fi done ;; #list -l or --list -l) FILE="${*}" for entry in "$dirfile"/* do echo "$entry" done ;; *) echo "`basename ${0}`:usage: [-f file] | [-d directory]" exit 1 # Command to come out of the program with status 1 ;; esac 中的JSON BODY发送POST请求。

它使用空的Json字符串到达​​服务器。

以下是建立连接并发送消息的代码:

CodeName

---

JSONObject json = new JSONObject();

class MyConnection extends ConnectionRequest {
    public Map<String, Object> results;

    @Override
    protected void readResponse(InputStream input) throws IOException {
        JSONParser jp = new JSONParser();
        results = jp.parseJSON(new InputStreamReader(input, "UTF-8"));
    }

    @Override
    protected void handleErrorResponseCode(int code, String message) {
        showError("The server returned the error code: " + code);
    }

    @Override
    protected void handleException(Exception err) {
        showError("There was a connection error: " + err);
    }

    @Override
    protected void postResponse() {
        try {
            json.put("AAA", "AAA");
            json.put("BBB", "BBB");
            json.put("CCC", "CCC");
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    protected void buildRequestBody(OutputStream os) throws IOException {
        os.write(json.toString().getBytes("UTF-8"));
    }
}

1 个答案:

答案 0 :(得分:1)

我不确定你的意图是什么,但看起来你误解了postResponse方法的目标。它与POST Web方法无关,只是在EDT完成响应后调用。因此,改变JSON值是无关紧要的。

此外,由于某种原因,您似乎使用了两个单独的JSON解析器。内置的一个和来自其中一个cn1lib的org.json

相关问题