douhan1860 2018-10-11 21:30
浏览 60

来自java的API调用,奇怪的结果?

So i am working on a project that involves a java application making calls to our API. It has to send data over GET and up until this point i could do it without any problems, however, i am trying to send a json string as one of the GET parameters.

Now, on the API, the GET variable will be empty when the call is made, however if i use 'print_r' or 'var_dump' on the API page when it is called, it magically works. (Obvious sarcasam)

I know that might sound confusing, that would be because it is. I havent had any problems making my calls up until this point.

My non-working code: (throws one of the errors.)

PHP

$checking = array(1, 2, 3);
$data = json_decode(base64_decode(urldecode($_GET['update'])), true);
if(!isset($data))
{
    exit(json_encode(array("Error"=>"Invalid data")));

}
if(count($data) != count($checking))
{
    exit(json_encode(array("Error"=>"Invalid amount: ")));
}

JAVA

List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();

for(ClientBase pb : this.clients)
{
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("id", pb.id);
    map.put("type", pb.getType());
    map.put("status", pb.getStatus() ? 1 : 0);
    list.add(map);
}

String out = JSONObject.valueToString(list);
String newout = new String(Base64.encode(out.getBytes()));
URLConnection conn = new URL("http://localhost/api/?key=" + Main.INSTANCE.APIKEY + "&update=" + URLEncoder.encode(newout, "UTF- 8")).openConnection();
InputStream is = conn.getInputStream();
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
res = s.hasNext() ? s.next() : "";

This results in one of the errors getting thrown due to the array being empty/null.

Now, if i change my PHP code to:

$checking = array(1, 2, 3);
$data = json_decode(base64_decode(urldecode($_GET['update'])), true);
var_dump($data);
if(!isset($data))
{
    exit(json_encode(array("Error"=>"Invalid data")));

}
if(count($data) != count($checking))
{
    exit(json_encode(array("Error"=>"Invalid amount: ")));
}

then the $data array is populated and no error gets thrown.

I really cant wrap my head around this one, Also i base64 encoded it and urlencoded it in an effort to get this to work.

Thanks for any help

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
    • ¥20 西门子S7-Graph,S7-300,梯形图
    • ¥50 用易语言http 访问不了网页
    • ¥50 safari浏览器fetch提交数据后数据丢失问题
    • ¥15 matlab不知道怎么改,求解答!!
    • ¥15 永磁直线电机的电流环pi调不出来
    • ¥15 用stata实现聚类的代码
    • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
    • ¥20 docker里部署springboot项目,访问不到扬声器
    • ¥15 netty整合springboot之后自动重连失效