|
|
|
ต้องการส่งค่าตัวแปร จาก Android ไป php เพื่อนำไป WHERE ฐานข้อมูล |
|
|
|
|
|
|
|
ต้องการส่งค่า text ไปที่ php แนะนำด้วยครับ
โค๊ดที่เขียนไว้
โค๊ด JAVA
Code (Android-Java)
public class SelectsubjActivity extends ListActivity {
LinearLayout layout;
public static final String url = "http://checkstd.w.pw/userlist.php";
public static String result = "";
List<User> subj = new ArrayList<User>();
private String text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selectsubj);
Bundle bundle = getIntent().getExtras();
text = bundle.getString("mmuser");
TextView tuser = (TextView) findViewById(R.id.user);
tuser.setText(text);
WebPageTask task = new WebPageTask();
task.execute();
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
/* User u = subj.get(position);
final Dialog dialog = new Dialog(SelectsubjActivity.this);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setPadding(10, 10, 10, 10);
TextView txt = new TextView(this);
txt.setText(u.subj_id);
linearLayout.addView(txt);
dialog.setContentView(linearLayout, new LayoutParams(400,
LinearLayout.LayoutParams.WRAP_CONTENT));
dialog.setTitle("User Detail");
dialog.setCancelable(true);
//dialog.show();
Intent i = new Intent(SelectsubjActivity.this, SelectsecActivity.class);
i.putExtra("d_subj", "txt");
startActivityForResult(i, 1);*/
startActivity(new Intent(SelectsubjActivity.this, SelectsecActivity.class));
}
public void showAllUser() {
ArrayAdapter<User> adapter = new ArrayAdapter<User>(this,
android.R.layout.simple_list_item_1, subj);
setListAdapter(adapter);
}
public String getData(String url, List<NameValuePair> nameValuePairs) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
BufferedReader in = null;
try {
HttpResponse response = httpclient.execute(httppost);
in = new BufferedReader(new InputStreamReader(response.getEntity()
.getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String page = sb.toString();
return page;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
return null;
}
public void parseXML(String xmlRecords) {
DocumentBuilder db = null;
try {
db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xmlRecords));
Document doc = null;
try {
doc = db.parse(is);
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
NodeList nodes = doc.getElementsByTagName("subject");
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
NodeList subj_id = element.getElementsByTagName("subj_id");
Element line = (Element) subj_id.item(0);
User u = new User();
u.subj_id = getCharacterDataFromElement(line);
NodeList name_subject = element.getElementsByTagName("name_subject");
line = (Element) name_subject.item(0);
u.name_subject = getCharacterDataFromElement(line);
subj.add(u);// add user to list
}
}
public String getCharacterDataFromElement(Element e) {
Node child = e.getFirstChild();
if (child instanceof CharacterData) {
CharacterData cd = (CharacterData) child;
return cd.getData();
}
return "";
}
class User {
public String name_subject;
public String subj_id;
@Override
public String toString() {
return name_subject;
}
}
private class WebPageTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... param) {
String xml_text = getData(url, null);
parseXML(xml_text);
return null;
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(SelectsubjActivity.this, "Load Succeed...",
Toast.LENGTH_SHORT).show();
showAllUser();
}
}
}
PHP
Code (PHP)
<?php
header("Content-type:text/xml");
echo "<?xml version='1.0' encoding='UTF-8'?>";
include("connect.php");
$username = $_POST['username'];
$sql = "SELECT * FROM subject WHERE username = '$username'";
$result = mysql_query($sql,$link);
echo "<data>";
while($row = mysql_fetch_array($result)){
echo "<subject>";
echo "<subj_id>".$row['subj_id']."</subj_id>";
echo "<name_subject>".$row['name_subject']."</name_subject>";
echo "</subject>";
}
echo "</data>";
?>
Tag : Mobile, JavaScript, Android, JAVA
|
|
|
|
|
|
Date :
2014-02-07 14:29:01 |
By :
lekn0335 |
View :
1724 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ต้องการ ส่งค่าตัวแปร text จาก android
เข้าไปตัวแปร $_POST['username'] ใน php
ต้องเพิ่มโค๊ดอย่างไรบ้างช่วยทีครับ
|
|
|
|
|
Date :
2014-02-07 15:06:30 |
By :
lekn0335 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|