【问题标题】:Json data from php server not working.来自 php 服务器的 Json 数据不起作用。
【发布时间】:2012-12-08 01:01:58
【问题描述】:

我正在上传 MYSQL 数据库中的数据,同时我想检索我插入的属性之一,以满足我成功上传的需求。当我第一次按下按钮时,它只将数据上传到服务器,并没有返回任何内容。当我再次点击按钮时,它会同时执行两个过程(插入和检索数据),所以我不能第一次以 json 对象的形式返回值。

这是我的php代码engrdatainsert.php

 <?php  
$sqlCon=mysql_connect("localhost","root","");
mysql_select_db("PeopleData");
 //Retrieve the data from the Android Post done by and Engr...
 $adp_no = $_REQUEST['adp_no'];
 $building_no = $_POST['building_no'];
 $contractor_name = $_POST['contractor_name'];
 $officer_name = $_POST['officer_name'];
  $area = $_POST['area'];

--------插入从 Android 接收到的值----------||

   $sql = "INSERT INTO engrdata (adp_no, building_no,area,contractor_name,officer_name)     VALUES('$adp_no', '$building_no', '$are',  '$contractor_name', '$officer_name')";

//--------现在查看Inserted data的事务状态---------||

 $q=mysql_query("SELECT adp_no FROM engrdata WHERE adp_no='$adp_no'");
   while($e=mysql_fetch_assoc($q))
    $output[]=$e;
print(json_encode($output));//conveting into json array
   mysql_close();
     ?>

我的 Android 代码

 public void insertdata()
   {
 InputStream is=null;
 String result=null;
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
        nameValuePairs.add(new BasicNameValuePair("adp_no",adp));//"34")); 
    nameValuePairs.add(new BasicNameValuePair("building_no",bldng));//"72"));
    nameValuePairs.add(new BasicNameValuePair("area",myarea));//"72"));
    nameValuePairs.add(new BasicNameValuePair("contractor_name",cntrct));//"72"));
    nameValuePairs.add(new BasicNameValuePair("officer_name",ofcr));//"72"));
    //http post
       try{
          HttpClient httpclient = new DefaultHttpClient();
          HttpPost httppost = new   HttpPost("http://10.0.2.2/androidconnection/engrdatainsert.php"); 
               httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
           HttpEntity entity = response.getEntity();
        is = entity.getContent();
        Log.i("postData", response.getStatusLine().toString());
    }

    catch(Exception e)
    {
        Log.e("log_tag", "Error in http connection "+e.toString());
    }   
 //convert the input strem into a string value
    try
      {
            BufferedReader reader = new BufferedReader(new    InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) 
            {
                    sb.append(line + "\n");
            }
            is.close();
            result=sb.toString();
    }
    catch(Exception e)
    {   Log.e("log_tag", "Error converting result "+e.toString());   }
    try
    {
        JSONArray jArray = new JSONArray(result);
        for(int i=0;i<jArray.length();i++)
        {
            JSONObject json_data = jArray.getJSONObject(i);
           Toast.makeText(this, "data is "+json_data.getString("adp_no")+"\n",   Toast.LENGTH_LONG).show();
           String return_val = json_data.getString("adp_no");
           if(return_val!=null)
           {
             Intent offff=new Intent(this,MainActivity.class);
              offff.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
              offff.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              //startActivity(offff);
           }
        }
    }
    //}
    catch(JSONException e)
    {      Log.e("log_tag", "Error parsing data "+e.toString());        }
   // return returnString;//*/
    }

【问题讨论】:

标签: php android mysql json


【解决方案1】:

如果您想在 android 中将 JSON 用于服务器目的。就像如果您想发送数据并从服务器检索响应,那么您必须以准确的方式使用 JSON,该方式已在此链接 Json in Android

中定义

【讨论】:

    【解决方案2】:

    在您的 PHP 代码中,您没有执行 INSERT 查询。你需要做这样的事情:

    --------插入从 Android 接收到的值----------||

    $sql = "INSERT INTO engrdata (adp_no, building_no,area,contractor_name,officer_name)     VALUES('$adp_no', '$building_no', '$are',  '$contractor_name', '$officer_name')";
    mysql_query($sql) or die(mysql_error());
    

    //--------现在查看Inserted data的事务状态---------||

    注意我添加的行,它实际执行查询。

    现在您当然应该将代码升级到mysqlimysqlPDO,因为不再支持 PHP mysql 包。

    【讨论】:

    • !我的这段代码是否无法在远程主机上工作,用于上传和检索数据,
    • 很抱歉我不能完全理解您的评论。
    • 我的意思是我正在开发一个应用程序,我将从手机上传一些数据到服务器,但目前我正在本地主机上进行这种做法,如果我将使用远程主机,就像任何网站一样,其中我已经存储了我的数据库,那么上面的代码在这种情况下是否可以工作
    • 没错,只是您需要更改这部分:$sqlCon=mysql_connect("localhost","root","");,因为您将不再访问本地主机上的服务器。
    猜你喜欢
    • 2010-10-31
    • 2011-01-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多