Android PHP Spinner问题 [英] Android PHP Spinner Issue
本文介绍了Android PHP Spinner问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
伙计我有我的应用程序,我使用httppost将一个产品名称从java文件发送到php文件,但我正在使用JSON从php文件中捕获响应,但问题是它总是从数据库给我一个错误的响应。 .....我正在通过选择微调器....发送产品名称在Android中
将代码发送到PHP的Android代码
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 {
// 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 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="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);
?>
推荐答案
host = ' localhost';
host='localhost';
uname = ' root';
uname='root';
pwd = ' root';
这篇关于Android PHP Spinner问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文