简体   繁体   English

PHP; 从外部网址(JAVA)获取帖子字符串

[英]PHP; get post string from external url (JAVA)

I need to send string data to an external url which is an API for clients, but I always get the server sending back an error, and then I checked the log in the server which is written in JAVA, it throws an exception 我需要将字符串数据发送到作为客户端API的外部URL,但是我总是使服务器发回错误,然后我检查了用JAVA编写的服务器中的日志,它引发了异常

"java.lang.NumberFormatException: For input string: "{"success":false,"errorCode":"500","message":"Unknown Error!","value":null,"totalPageCount":0,"footerValue":null}" “ java.lang.NumberFormatException:对于输入字符串:” {“ success”:false,“ errorCode”:“ 500”,“ message”:“未知错误!”,“ value”:null,“ totalPageCount”:0,“ footerValue “:空}”

and here's my piece of php code: 这是我的一段PHP代码:

$context = stream_context_create(array( 
'http' => array( 
    'method' => 'POST', 
    'header' => "Content-type: application/x-www-form-urlencoded\r\n", 
    'content' => $params  
    ), 
)); 
$received = file_get_contents(URL, false, $context); 
echo $received.'<br>';

I'm pretty sure the data I send to the server is in correct format as I have the access to the log, and I know that all response parameters from the server are in JSON, UTF-8 and post method, is there something I missed? 我可以确定发送到服务器的数据的格式正确,因为我可以访问日志,并且我知道服务器的所有响应参数都采用JSON,UTF-8和post方法,是否有错过了什么?

If more information needed please let me know, thanks. 如果需要更多信息,请告诉我,谢谢。

You need to parse the string. 您需要解析字符串。 You are getting NumberFormatException and this is because your server is receiving data which is not a numeric. 您正在获取NumberFormatException,这是因为您的服务器正在接收非数字数据。

Integer.parseInt() // in java

You can parse the content in server side this way or send numeric data. 您可以通过这种方式在服务器端解析内容或发送数字数据。

Another method: 另一种方法:

Integer.valueOf() // in java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM