来自php服务器的Json数据不起作用。

时间:2022-10-17 10:46:29

I am uplodaing data in MYSQL data base and at the same time I want to retrieve one of the attribute which I have inserted, for the satisfaction of my successful upload. when I press the button for first time then, it only upload the data to the server, and return nothing. Again when I hit the button then it does both the processs(insertion and retrieving data), so I can't return value at a first time in form of json object.

我在MYSQL数据库中提升数据,同时我想检索我插入的一个属性,以满足我的成功上传。当我第一次按下按钮时,它只将数据上传到服务器,并且什么都不返回。再次当我按下按钮然后它执行两个过程(插入和检索数据),所以我不能在第一次以json对象的形式返回值。

This is my php code engrdatainsert.php

这是我的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'];

-------------------insert the received value from an Android----------||

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

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

//--------Now check out the transaction status of the 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();
     ?>

My Android code

我的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;//*/
    }

2 个解决方案

#1


1  

In you PHP code, you are not executing the INSERT query. You need to do something like this:

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

-------------------insert the received value from an Android----------||

-------------------从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());

//--------Now check out the transaction status of the Inserted data---------||

// --------现在查看插入数据的交易状态--------- ||

Notice the line I added, which actually executes the query.

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

Now of course you should upgrade your code to mysqli or mysqlPDO since the PHP mysql package is not supported anymore.

当然,您应该将代码升级到mysqli或mysqlPDO,因为不再支持PHP mysql包。

#2


1  

If you want to use JSON in android for server purposes. like if you want to send data and retrieve a response from the server, then You have to use the JSON in accurate manner which have been defined in this link Json in Android

如果你想在android中使用JSON用于服务器目的。就像你想要发送数据并从服务器检索响应一样,那么你必须以准确的方式使用JSON,这已经在Android链接Json中定义了

#1


1  

In you PHP code, you are not executing the INSERT query. You need to do something like this:

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

-------------------insert the received value from an Android----------||

-------------------从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());

//--------Now check out the transaction status of the Inserted data---------||

// --------现在查看插入数据的交易状态--------- ||

Notice the line I added, which actually executes the query.

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

Now of course you should upgrade your code to mysqli or mysqlPDO since the PHP mysql package is not supported anymore.

当然,您应该将代码升级到mysqli或mysqlPDO,因为不再支持PHP mysql包。

#2


1  

If you want to use JSON in android for server purposes. like if you want to send data and retrieve a response from the server, then You have to use the JSON in accurate manner which have been defined in this link Json in Android

如果你想在android中使用JSON用于服务器目的。就像你想要发送数据并从服务器检索响应一样,那么你必须以准确的方式使用JSON,这已经在Android链接Json中定义了