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
name.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView {
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()
{
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);
HttpEntity entity = response.getEntity();
is1= entity.getContent();
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();
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]);
}
PHP COde
<?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=$_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;
}
print(json_encode($cl));
mysql_close($con);
?>