2013-06-23 50 views
-2
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 ‘對不起任何不正確的在我的談話中,我的英語也不好。’在logcat的的Android發送到PHP(httppost)「未定義指數:txtProvinceId」請幫我

<?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); 
?> 

PHP響應 「未定義指數:txtProvinceId在amphur.php第3行」

請幫助我! 「對不起,我的對話不正確,我的英語不好。」

回答

0

您絕不會將您的params添加到查詢字符串中。您可以使用URLEncodedUtils.format()輕鬆進行格式化:

import org.apache.http.client.utils.URLEncodedUtils; 
... 

private String getJsonFromUrl(String strUrl,ArrayList<NameValuePair> params) { 
    String queryString = URLEncodedUtils.format(params, null); 
    URL url = new URL(strUrl + "?" + queryString); 
    ... 
} 
+0

「謝謝。沒有你,我無法做到這一點「 –