2

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.

This is my php code 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----------||

   $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

 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;//*/
    }
Sahil Mahajan Mj
  • 11,033
  • 8
  • 53
  • 100
Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81
  • See this link [JSON IN ANDROID][1] May you will find your solution here in this link [1]: http://stackoverflow.com/questions/2818697/sending-and-parsing-json-in-android – Syed Nizam May 02 '13 at 07:52

2 Answers2

1

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

-------------------insert the received value from an 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.

jeffery_the_wind
  • 17,048
  • 34
  • 98
  • 160
  • ! would my this code be not able to work on remote host,for uploading and retrieving data, – Pir Fahim Shah Dec 20 '12 at 14:00
  • 1
    I'm sorry I can't quite understand your comment here. – jeffery_the_wind Dec 20 '12 at 14:50
  • i meant that i am developing an apps where i will upload some data to server from phone, but currently i am working this practice on localhost, if i will used a remote host, like any website, where i have stored my Data Base, then would the above code will work in that case – Pir Fahim Shah Dec 20 '12 at 15:41
  • that's right, except you will need to change this part: `$sqlCon=mysql_connect("localhost","root","");` since you will no longer be accessing the server at localhost. – jeffery_the_wind Dec 20 '12 at 16:40
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

Community
  • 1
  • 1
Syed Nizam
  • 21
  • 4