[英]PHP post array to java servlet using cUrl
I'm trying to post the following example array, from php, to a java servlet: 我正在尝试将以下示例数组从php发布到Java servlet:
$tags = $tagger->tag($nlquery);
$js_string = json_encode($tags);
echo $js_string
result: 结果:
{"Example","NN"},{"1","NN"}
cUrl code: 网址代码:
$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 code: 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]);
}
However this results in a NullPointerException
on the java-side at the jsonData.length
line. 但是,这会在jsonData.length
行的Java端导致NullPointerException
。 Am I passing the $js_string wrong from the php side? 我是从PHP端传递$ js_string错误吗? Or is their something wrong with the java-side code? 还是Java端代码有问题?
Update: 更新:
Using the following code: 使用以下代码:
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]);
}
returns the right result {"Example","NN"},{"1","NN"}
however, now I can't loop through it because it doesn't see it as a multidimensional array. 返回正确的结果{"Example","NN"},{"1","NN"}
但是,现在我无法遍历它,因为它不将其视为多维数组。 This is the code I worked with before using the servlet (static, for developing purposes) 这是我在使用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] );
}
}
Maybe not so effective but atleast it works: 也许不是那么有效,但至少可以起作用:
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]);
}
Results in a 2 dimensional array, just like the above static example. 就像上面的静态示例一样,将得到一个二维数组。
Try this 尝试这个
BufferedReader br = new BufferedReader(new InputStreamReader (req.getInputStream()));
String str = br.readLine();
System.out.println(str);
// JSONObject data = new JSONObject(str);
This worked for me. 这对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.