I am working on a Android APP using MIT APP Inventor.
I have 2 phones, that comunicate using a php script in my server.
It´s very simple, using TinyWebDB component, I need to post and get a text string.
Each phone creates a text file using the php (aaa.txt and bbb.txt) "aaa" and "bbb" is the content of the variable $tag on each pone.
I create the text files correctly, and I post the values on them, but when I try to get the value from the other phone, I get the error:
Value br of type java.lang.String cannot be converted to JSONArray
There is my php script:
<?php
$postUrl=$_SERVER["REQUEST_URI"];
if(strpos($postUrl,'storeavalue')){
// Storing a Value
// Get that tag
$tag = trim($_POST["tag"]);
// Get the value
$value = trim($_POST["value"]);
// Create the text file
$myFile = "$tag.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
// str_replace, delete "
fwrite($fh, str_replace('"', '', $value));
fclose($fh);
} else {
// Retrieving a Value
$tag = trim($_GET["tag"]);
$myFile = "$tag.txt"; //there happens the error
$fh = fopen($myFile, 'r');
$theData = fgets($fh);
fclose($fh);
$resultData = array("VALUE",$tag,array($theData));
$resultDataJSON = json_encode($resultData);
echo $resultDataJSON;
}
?>
So, when retrieving a value, the variable $tag should contain "aaa.txt" or "bbb.txt".
If I use the variable $tag to designate the file, I get the error, but if I change to this:
$myFile = "aaa.txt";
or
$myFile = "bbb.txt";
it works perfect.
Can anybody help me please?
Thx for your time and sorry for bad english :/
---------EDIT---------
I solved the problem, it was really simple, I was using the POST method to store value and the GET method to retrieve it. Now I only use the POST method for both and it work perfect.
just change the line:
// Retrieving a Value
$tag = trim($_POST["tag"]);