在android中将参数传递给php
我在Android中将参数传递给PHP时遇到问题 我有两项活动。其中一个读取我的数据库,并用该数据库表中的行填充列表。它很好用。然后,当我单击ListItem时,我会将特定项目的id发送到另一个活动:在android中将参数传递给php,php,android,Php,Android,我在Android中将参数传递给PHP时遇到问题 我有两项活动。其中一个读取我的数据库,并用该数据库表中的行填充列表。它很好用。然后,当我单击ListItem时,我会将特定项目的id发送到另一个活动: String identyfikator = ((TextView) view.findViewById(R.id.identyfikator)).getText().toString(); Intent in = new Intent(getApplicationContext(), Przys
String identyfikator = ((TextView)
view.findViewById(R.id.identyfikator)).getText().toString();
Intent in = new Intent(getApplicationContext(), PrzystanekKierunek.class);
in.putExtra(TAG_IDENTYFIKATOR, identyfikator);
startActivity(in);
package com.example.mybusstop;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
public class PrzystanekKierunek extends ListActivity {
String identyfikator;
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> wyszukaniaList;
private static String url_przystanek_kierunek = "http://10.0.2.2/mybusstop_php/przystanekkierunek.php";
private static final String TAG_WYSZUKANIA = "wyszukania";
private static final String TAG_IDENTYFIKATOR = "identyfikator";
private static final String TAG_NAZWA = "nazwa";
JSONArray wyszukania = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.przystanekkierunek);
wyszukaniaList = new ArrayList<HashMap<String, String>>();
Intent i = getIntent();
identyfikator = i.getStringExtra(TAG_IDENTYFIKATOR);
new LoadKierunek().execute();
/* ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String identyfikator = ((TextView) view.findViewById(R.id.identyfikator)).getText().toString();
Intent in = new Intent(getApplicationContext(), PrzystanekKierunek.class);
in.putExtra(TAG_IDENTYFIKATOR, identyfikator);
startActivity(in);
}
});
*/
}
class LoadKierunek extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(PrzystanekKierunek.this);
pDialog.setMessage("Wyszukiwanie...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("identyfikator", identyfikator));
JSONObject json = jParser.makeHttpRequest(url_przystanek_kierunek, "GET", params);
try {
wyszukania = json.getJSONArray(TAG_WYSZUKANIA);
for (int i = 0; i < wyszukania.length(); i++) {
JSONObject c = wyszukania.getJSONObject(i);
String identyfikator = c.getString(TAG_IDENTYFIKATOR);
String nazwa = c.getString(TAG_NAZWA);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_IDENTYFIKATOR, identyfikator);
map.put(TAG_NAZWA, nazwa);
wyszukaniaList.add(map);
}
}
catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
ListAdapter adapter = new SimpleAdapter(
PrzystanekKierunek.this, wyszukaniaList,
R.layout.list_item, new String[] {TAG_NAZWA, TAG_IDENTYFIKATOR},
new int[] {R.id.nazwaItem, R.id.identyfikator});
setListAdapter(adapter);
}
});
}
}
}
在第二个活动中,我读取了发送id,这是我的问题。我已经检查过这是否有效
Intent i = getIntent();
identyfikator = i.getStringExtra(TAG_IDENTYFIKATOR);
它是有效的。我已经用identnyfikator更新了editText,它显示了正确的id。但是当我尝试将它发送到我的PHP时,它返回空白活动。这个代码有什么问题?
以下是第二项活动:
String identyfikator = ((TextView)
view.findViewById(R.id.identyfikator)).getText().toString();
Intent in = new Intent(getApplicationContext(), PrzystanekKierunek.class);
in.putExtra(TAG_IDENTYFIKATOR, identyfikator);
startActivity(in);
package com.example.mybusstop;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
public class PrzystanekKierunek extends ListActivity {
String identyfikator;
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> wyszukaniaList;
private static String url_przystanek_kierunek = "http://10.0.2.2/mybusstop_php/przystanekkierunek.php";
private static final String TAG_WYSZUKANIA = "wyszukania";
private static final String TAG_IDENTYFIKATOR = "identyfikator";
private static final String TAG_NAZWA = "nazwa";
JSONArray wyszukania = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.przystanekkierunek);
wyszukaniaList = new ArrayList<HashMap<String, String>>();
Intent i = getIntent();
identyfikator = i.getStringExtra(TAG_IDENTYFIKATOR);
new LoadKierunek().execute();
/* ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String identyfikator = ((TextView) view.findViewById(R.id.identyfikator)).getText().toString();
Intent in = new Intent(getApplicationContext(), PrzystanekKierunek.class);
in.putExtra(TAG_IDENTYFIKATOR, identyfikator);
startActivity(in);
}
});
*/
}
class LoadKierunek extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(PrzystanekKierunek.this);
pDialog.setMessage("Wyszukiwanie...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("identyfikator", identyfikator));
JSONObject json = jParser.makeHttpRequest(url_przystanek_kierunek, "GET", params);
try {
wyszukania = json.getJSONArray(TAG_WYSZUKANIA);
for (int i = 0; i < wyszukania.length(); i++) {
JSONObject c = wyszukania.getJSONObject(i);
String identyfikator = c.getString(TAG_IDENTYFIKATOR);
String nazwa = c.getString(TAG_NAZWA);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_IDENTYFIKATOR, identyfikator);
map.put(TAG_NAZWA, nazwa);
wyszukaniaList.add(map);
}
}
catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
ListAdapter adapter = new SimpleAdapter(
PrzystanekKierunek.this, wyszukaniaList,
R.layout.list_item, new String[] {TAG_NAZWA, TAG_IDENTYFIKATOR},
new int[] {R.id.nazwaItem, R.id.identyfikator});
setListAdapter(adapter);
}
});
}
}
}
package com.example.mybustop;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入org.apache.http.NameValuePair;
导入org.apache.http.message.BasicNameValuePair;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.ListActivity;
导入android.app.ProgressDialog;
导入android.content.Intent;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.widget.ListAdapter;
导入android.widget.simpledapter;
公共类PrzystanekKierunek扩展ListActivity{
字符串标识符;
私人对话;
JSONParser jParser=新的JSONParser();
ArrayList wyszukaniaList;
私有静态字符串url_przystanek_kierunek=”http://10.0.2.2/mybusstop_php/przystanekkierunek.php";
私有静态最终字符串标记_WYSZUKANIA=“WYSZUKANIA”;
私有静态最终字符串标记_identifikator=“identifikator”;
私有静态最终字符串标记_-NAZWA=“NAZWA”;
JSONArray wyszukania=null;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.przystanekkierunek);
wyszukaniaList=新的ArrayList();
Intent i=getIntent();
identityficator=i.getStringExtra(标记为identityficator);
新的LoadKierunek().execute();
/*ListView lv=getListView();
lv.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
字符串identifIkator=((TextView)view.findViewById(R.id.identifIkator)).getText().toString();
Intent in=new Intent(getApplicationContext(),PrzystanekKierunek.class);
in.putExtra(标签标识器,标识器);
星触觉(in);
}
});
*/
}
类LoadKierunek扩展了异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(PrzystanekKierunek.this);
setMessage(“Wyszukiwanie…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
受保护的字符串doInBackground(字符串…args){
List params=new ArrayList();
参数添加(新的BasicNameValuePair(“IdentityFicator”,IdentityFicator));
JSONObject json=jParser.makeHttpRequest(url_przystanek_kierunek,“GET”,参数);
试一试{
wyszukania=json.getJSONArray(TAG_wyszukania);
对于(int i=0;i
以及PHP脚本:
<?php
$response = array();
require_once __DIR__ . '/db.connecting.php';
$db = new DB_CONNECT();
//if (isset($_GET["identyfikator"])) {
$ide = $_GET['identyfikator'];
$result = mysql_query("SELECT * FROM przystanek WHERE identyfikator=$ide") or die(mysql_error());
if (mysql_num_rows($result) > 0) {
$response["wyszukania"] = array();
while ($row = mysql_fetch_array($result)) {
$kierunek = array();
$kierunek["id"] = $row["Identyfikator"];
$kierunek["nazwa"] = $row["Nazwa_przystanek"];
$kierunek["gps_x"] = $row["Lokalizacja_gps_x"];
$kierunek["gps_y"] = $row["Lokalizacja_gps_y"];
array_push($response["wyszukania"], $kierunek);
}
$response["success"] = 1;
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "Błąd wyszukania!";
echo json_encode($response);
}
//}
?>
如果将php代码替换为:
<?php
$res = json_decode(stripslashes($_GET['identyfikator']),true);
echo $res
?>
我猜JSONParser是您创建的一个实用程序?(JSONParser.makeHttpRequest(url_przystanek_kierunek,“GET”,params);)你能发布这些代码吗?是的,这是另一个类,负责将我的android与php/mysql连接起来。但我已经解决了我的问题。问题在于我的变量名。我已更改$kierunek[“id”]=$row[“identifikator”];至$kierunek[“identifikator”]=$row[“identifikator”];现在一切都好了。谢谢各位。我不能在8小时前重播我自己的帖子,所以我只能评论别人的帖子。