douzhankui0758 2014-09-30 22:06
浏览 56

Android错误:java.lang.String类型的值br无法转换为JSONArray

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"]);
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 测距传感器数据手册i2c
    • ¥15 RPA正常跑,cmd输入cookies跑不出来
    • ¥15 求帮我调试一下freefem代码
    • ¥15 matlab代码解决,怎么运行
    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法