【问题标题】:PHP post array to java servlet using cUrlPHP 使用 cUrl 将数组发布到 java servlet
【发布时间】:2015-05-29 12:37:36
【问题描述】:

我正在尝试将以下示例数组从 php 发布到 java servlet:

$tags = $tagger->tag($nlquery);
$js_string = json_encode($tags);    
echo $js_string

结果:

{"Example","NN"},{"1","NN"}

cUrl 代码:

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "http://localhost:8080/first/SPARQL");
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
        curl_setopt($ch, CURLOPT_POSTFIELDS, $js_string);                                                                  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($js_string)));
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        $response = curl_exec($ch);
        curl_close($ch);
        echo $response;

java servlet 代码:

protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        // TODO Auto-generated method stub
        res.setContentType("text/html");
        res.addHeader("Access-Control-Allow-Origin", "*");
        PrintWriter out = res.getWriter();
        String jsonData[];

        jsonData= req.getParameterValues("js_string");
        for(int i = 0; i < jsonData.length; i++)
        {
        System.out.println(jsonData[i]);
        }

但是,这会导致在 jsonData.length 行的 java 端出现 NullPointerException。我是否从 php 端传递了错误的 $js_string ?还是他们的 java 端代码有问题?

更新:
使用以下代码:

BufferedReader br = new BufferedReader(new InputStreamReader (req.getInputStream()));
        String str = br.readLine();
        String[] jsondata = new String[]{str}; 
        int i;
        for(i=0; i < jsondata.length;i++){
            System.out.println(jsondata[i]);
        }

返回正确的结果{"Example","NN"},{"1","NN"} 但是,现在我无法循环遍历它,因为它不将其视为多维数组。这是我在使用 servlet 之前使用的代码(静态,用于开发目的)

String[][] phpsearch ={{"Example","NN"},{"1","NN"}};
for(int i=0; i<phpsearch.length; i++) {
                System.out.println("loop "+i);
                if(phpsearch[i][1]=="NN"){
                    result=searchLabel(phpsearch[i][0].toLowerCase());
                    System.out.println("array "+phpsearch[i][0] );
}
}

也许不是那么有效,但至少它有效:

BufferedReader br = new BufferedReader(new InputStreamReader (req.getInputStream()));
        String str = br.readLine();
        str = str.replace(",", "|");
        str = str.replace("}|{", ",");
        str = str.replace("}", "");
        str = str.replace("{", "");

        String[] rows = str.split(",");
        System.out.println("rows");
        for(int i=0; i<rows.length; i++) {
            System.out.println("loop "+i);
            System.out.println(rows[i]);
            //System.out.println(jsondata[i][1]);
        }

        String[][] jsondata = new String[rows.length][]; 
        int r = 0;
        for (String row : rows) {
            jsondata[r++] = row.split("\\|");
        }
        System.out.println("jsondata");
        for(int i=0; i<jsondata.length; i++) {
            System.out.println("loop "+i);
            System.out.println(jsondata[i][0]);
            System.out.println(jsondata[i][1]);

            //System.out.println(jsondata[i][1]);
        }

生成一个二维数组,就像上面的静态示例一样。

【问题讨论】:

    标签: java php json servlets curl


    【解决方案1】:

    试试这个

    BufferedReader br = new BufferedReader(new InputStreamReader (req.getInputStream()));
    String str = br.readLine();
    System.out.println(str);
    // JSONObject data = new JSONObject(str);
    

    这对我有用。

    【讨论】:

    • 感谢您的回复,我试过了,但它说:构造函数 JSONObject(String) 是未定义的
    • @vincentkleine 删除最后一行并打印str 并检查它是否有数据
    • 啊是的,它有数据!我可以在数组中使用它,但无论如何我可以在二维数组中使用它吗?
    • @vincentkleine 我认为在这里使用org.json.simple.JSONObject 会是有益的
    猜你喜欢
    • 2014-04-18
    • 1970-01-01
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-14
    相关资源
    最近更新 更多