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