public class DialogSelectAmphurActivity extends Activity {
private final String TAG = "internet";
private ListView listview_province;
ArrayList<HashMap<String, String>> myList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
String strUrl =("http://192.168.1.4/test_projectEnd/amphur.php");
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_select_province_dialog);
new AsyncDownload().execute(strUrl);
}
public String getData(String strUrl, ArrayList<NameValuePair> params){
String jString;
HashMap<String, String> map;
String sProvince_id = getIntent().getStringExtra("provinceId");
params.add(new BasicNameValuePair("txtProvinceId",sProvince_id));
try {
jString = getJsonFromUrl(strUrl, params);
JSONArray jArray = new JSONArray(jString);
Log.d(TAG, jArray +","+ params);
for(int i =0; i< jArray.length(); i++)
{
JSONObject jObj = jArray.getJSONObject(i);
String sAmphur_id = jObj.getString("AMPHUR_ID");
String sAmphur_name = jObj.getString("AMPHUR_NAME");
map = new HashMap<String, String>();
map.put("amphur_id", sAmphur_id);
map.put("amphur_name", sAmphur_name);
myList.add(map);
Log.d(TAG, sAmphur_id + sAmphur_name);
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
private void showProvince(){
ListView listView = (ListView) findViewById(R.id.listView_province1);
ListAdapter adapter = new SimpleAdapter(this, myList, R.layout.row_layout_select_province,
new String[]{"amphur_id","amphur_name"}, new int[]{R.id.textView_province_id,R.id.textView_province_name});
listView.setAdapter(adapter);
}
private String getJsonFromUrl(String strUrl,ArrayList<NameValuePair> params)throws IOException{
URL url = new URL(strUrl);
HttpPost httpPost = new HttpPost(strUrl);
try {
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
Log.d(TAG, params+"check");
httpCon.setRequestMethod("GET");
httpCon.setConnectTimeout(6*1000);
int responseCode = httpCon.getResponseCode();
Log.d(TAG, "The response is" + responseCode);
if(responseCode == HttpsURLConnection.HTTP_OK){
Log.d(TAG, "size" + httpCon.getContentLength());
InputStream ins = httpCon.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(ins,"UTF-8"));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null){
response.append(line);
response.append("\n");
Log.d(TAG, line);
}
rd.close();
return response.toString();
}
} catch (Exception ex) {
Log.d(TAG,"Problem reading"+ ex.getLocalizedMessage());
}
return null;
}
private class AsyncDownload extends AsyncTask<String, Void, String>{
ProgressDialog pd;
@Override
protected void onPreExecute(){
pd = ProgressDialog.show(DialogSelectAmphurActivity.this, "Download", "Downloading....");
}
protected String doInBackground(String... Params){
String data = getData(strUrl, params);
return null;
}
protected void onPostExecute(String result){
pd.dismiss();
showProvince();
}
}
}
我将 txtprovinceId 发送到 php “对不起,我的对话有任何不正确之处,我的英语不好。”
<?php
$provinceid = trim($_GET["txtProvinceId"]);
require("libs/connection_to_abc.php");
mysql_query("SET character_set_results=utf8");
mysql_query("SET character_set_client=utf8");
mysql_query("SET character_set_connection=utf8");
$strSQL = "SELECT amphur.* FROM province,amphur
WHERE province.PROVINCE_ID = amphur.PROVINCE_ID
AND province.PROVINCE_ID ='$provinceid' ";
$objQuery = mysql_query($strSQL);
$intNumField = mysql_num_fields($objQuery);
$resultArray = array();
while($obResult = mysql_fetch_array($objQuery))
{
$arrCol = array();
for($i=0;$i<$intNumField;$i++)
{
$arrCol[mysql_field_name($objQuery,$i)] = $obResult[$i];
}
array_push($resultArray,$arrCol);
}
mysql_close($link);
echo json_encode($resultArray);
?>
Logcat 中的 php 响应“未定义的索引:amphur.php 中的 txtProvinceId 第 3 行”
请帮帮我 !“对不起,我的谈话有误,我的英语不好。”