Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Guys i have my application in that i am sending one product name from java file to php file using httppost but and i am catching response from php file using JSON but the problem is it is always give me a wrong response from database ......i am sending product name by selecting spinner....in android

Android code to Send String to PHP

Java
name.setOnItemSelectedListener(new OnItemSelectedListener()
{
	@Override
	public void onItemSelected(AdapterView	{
		// TODO Auto-generated method stub
		
		final String product_name=name.getItemAtPosition(position).toString();
		System.out.println("Position ::"+position);
		System.out.println("you have selected::::"+product_name);
		new Thread(new Runnable()
		{
             public void run()
             {   
            	 // TODO Auto-generated method stub
            	 try
            	 {	
            		
            		 HttpClient httpclient = new DefaultHttpClient();
            		 HttpPost httppost = new HttpPost("http://192.168.32.203/android_connect/productdetails.php");
            		nameValuePairs = new ArrayList<namevaluepair>(1);
            		  nameValuePairs.add(new BasicNameValuePair("name",product_name.trim()));
            		  System.out.println("String Send To PHP file is :::"+product_name);
            		  httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            		 HttpResponse response = httpclient.execute(httppost);
            		// Log.e("Fail 1", "3");

            		 HttpEntity entity = response.getEntity();
            		// Log.e("Fail 1", "4");

            		 is1= entity.getContent();
            	//	 System.out.println("after executing entity::"+is1);
            		 Log.e("Pass 1", "connection success ");
            		 
  	 
            	 }
            	 catch(Exception e)
            	 {
            		 Log.e("ERROR", e.toString());
            		 Toast.makeText(getApplicationContext(), "Invalid IP Address",Toast.LENGTH_LONG).show();
            		 finish();
            	 }   
            	
    
           }
         }).start();  
           
           try
			{
				BufferedReader reader = new BufferedReader(new InputStreamReader(is1,"iso-8859-1"),8);
				StringBuilder sb1 = new StringBuilder();
				while ((line1 = reader.readLine()) != null)
				{
					sb1.append(line1 + "\n");
				}
				is1.close();
				result1 = sb1.toString();
				// Toast.makeText(getApplicationContext(), "2nd try",Toast.LENGTH_LONG).show();
				System.out.println(result1);
			}
			catch(Exception e)
			{
				Log.e("ERROR 2::", e.toString());
				
			}    
				try
				{
					JSONArray JA=new JSONArray(result1);
					JSONObject json= null;
					temp = new String[JA.length()];  
					serialno1 = new String[JA.length()];  
					age1 = new String[JA.length()];  
					plan1 = new String[JA.length()];  
					mrp1 = new String[JA.length()];  
					for(int i=0;i<ja.length();i++)>
					{
						json=JA.getJSONObject(i);
						temp[i] = json.getString("appliance_model_no");
						serialno1[i] = json.getString("appliance_serial_no");
						age1[i] = json.getString("appliance_age");
						plan1[i] = json.getString("appliance_plan");
						mrp1[i] = json.getString("appliance_mrp");
						System.out.print("Data in Temp Array::"+temp[i]+"\t");
					}
					for(int i=0;i<temp.length;i++)>
					{
						list1.add(temp[i]);
						list2.add(serialno1[i]);
						list4.add(age1[i]);
						list5.add(plan1[i]);
						list6.add(mrp1[i]);
						//System.out.print(list1.get(i)+"\t");
					}


PHP COde
PHP
<?php

$host='localhost';

$uname='root';
$pwd='root';
$db='mydatabase';
$con = mysql_connect($host,$uname,$pwd) or die("connection failed");
mysql_select_db($db,$con) or die("db selection failed");
//$name="SPLITAC";
$name=$_POST['name'];
$r=mysql_query("select appliance_model_no ,appliance_serial_no,appliance_age,appliance_plan,appliance_mrp from productdetails where appliance_name='".$name."' ",$con);
while($row=mysql_fetch_array($r))
{
$cl[]=$row;
//echo $fin."<br>";
}
print(json_encode($cl));
mysql_close($con);
?>
Posted
Updated 6-Aug-13 0:06am
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900