【问题标题】:Error occured: org.json.JSONException: End of input at character 3 When I send JSON TO WEB SERVER发生错误:org.json.JSONException: End of input at character 3 当我将 JSON 发送到 WEB SERVER
【发布时间】:2017-10-16 14:10:21
【问题描述】:

我正在从 android 向 Web 服务器发送 JSON 数据。我有数组列表 CURRENCY_RATE_LST,我想将它发送到服务器。 下面是我的代码

安卓代码:

    for(int i=0;i<currency_rate_lst.size();i++)
    {
        try
        {
            currency_rate_JSON .put(""+currency_code_lst.get(i), currency_rate_lst.get(i));
        }
        catch (JSONException e) 
        {
            e.printStackTrace();
            Log.e("JSON ENCODIG ERROR ",""+e.getMessage());
        }
    }

        final JSONObject JSON = new JSONObject();
        try 
        {
            JSON.put("currency_rate_JSON", currency_rate_JSON);
        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

         AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() 
            {
                String message="Nothing happend";
             @Override
             protected String doInBackground(Void... params) 
                {

              InputStream is=null;
              String result=null;
               ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
               nameValuePairs.add(new BasicNameValuePair("currency_rate_json",""+String.valueOf(JSON)));
              '
              '
              .
               }

PHP 代码:

//Receive JSON
 $JSON_Received = $_POST["currency_rate_json"]; 
 //Decode Json
 $obj = json_decode($JSON_Received, true);
  foreach ($obj['currency_rate_JSON'] as $key => $value) 
    {
        //echo "<br>------" . $key . " => " . $value;

       $currency_rate=$value;
       $currency_code=$key;

  //$con=$GLOBALS['con'];

  $sql2="UPDATE  `pirzawbh_currency_exchange`.`latest_currency_rate_table` SET currency_rate = '$currency_rate' 
  WHERE `latest_currency_rate_table`.`currency_code` ='$currency_code';";

 if (!mysql_query($sql2, $con))
 {
//echo "\n =".mysql_error($con);
  //  echo"\n errro occured";

  }
//else
    //echo"\n Updated row id = ".$rowID." Successfuly";;

 }

【问题讨论】:

  • 我看到了 php 方面的几个问题。我不是 android 工程师,所以如果没有你的输出示例,我不能说那一方是否正在构建正确的 json。
  • @Randall 兄弟有什么问题,你可以告诉我
  • 你能把print_r($_POST);添加到你的php顶部来调试post中的内容吗?
  • 删除了我的答案,因为我相信我认为您的 json 对象格式有误。对不起。由于我也不知道 android 代码,所以我会走开并停止处理您的问题;)

标签: php android mysql json


【解决方案1】:

我自己找到了解决方案,只需更改此行

 nameValuePairs.add(new BasicNameValuePair("currency_rate_json",""+String.valueOf(JSON)));

nameValuePairs.add(new BasicNameValuePair("currency_rate_json",""+currency_rate_JSON.toString()));

【讨论】:

    猜你喜欢
    • 2018-10-14
    • 1970-01-01
    • 2014-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-08
    • 1970-01-01
    相关资源
    最近更新 更多