綁定帳號登入

Android 台灣中文網

打印 上一主題 下一主題

[求助] 關於用AlertDialog來傳用資料到Http

[複製連結] 查看: 1323|回覆: 4|好評: 0
跳轉到指定樓層
樓主
darklight | 收聽TA | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
發表於 2013-11-5 10:02

馬上加入Android 台灣中文網,立即免費下載應用遊戲。

您需要 登錄 才可以下載或查看,沒有帳號?註冊

x
很抱歉小弟最近遇到一個大問題!

就是無法使用AlertDialog來傳資料到Http  已經研究很久了! 一樣無法使用...

希望求解!

這是程式碼片段


private OnClickListener buttonListener = new OnClickListener(){
                @Override
                public void onClick(View view) {
                    switch(view.getId()){
                    case R.id.btn_register:
                            LayoutInflater inflater = LayoutInflater.from(driver.this);
                    final View v = inflater.inflate(R.layout.driver_register, null);
                    new AlertDialog.Builder(driver.this)
                    .setTitle("請輸入你的名字")
                    .setView(v)
                    .setPositiveButton("確定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            EditText account = (EditText) (v.findViewById(R.id.driver_account));
                            EditText pwd = (EditText) (v.findViewById(R.id.driver_pwd));
                            EditText pwd2 = (EditText) (v.findViewById(R.id.driver_pwd2));
                            EditText email = (EditText) (v.findViewById(R.id.driver_email));
                            EditText tel = (EditText) (v.findViewById(R.id.driver_tel));
                            EditText carid = (EditText) (v.findViewById(R.id.driver_carid));

                             String account_s = account.getText().toString();
                             String pwd_s = pwd.getText().toString();
                             String pwd2_s = pwd2.getText().toString();
                             String email_s = email.getText().toString();
                             String tel_s = tel.getText().toString();
                             String carid_s = carid.getText().toString();       
                                                                                                
                            Thread t = new Thread(new sendPostRunnable(account_s,pwd_s,pwd2_s,email_s,tel_s,carid_s));
                                            t.start();       

                        }
                    })
                    .show();
                    break;
                }
               }
                
         };



另外是sendPostRunnable的這邊

class sendPostRunnable implements Runnable {

                String driver_account1 = null;
                String driver_pwd1 = null;
                String driver_email1 = null;
                String driver_tel1 = null;
                String driver_carid1 = null;

                // 建構子,設定要傳的字串
                public sendPostRunnable(String driver_account1,String driver_pwd1,
                        String driver_email1,
                        String driver_tel1,
                        String driver_carid1)
                {
                        this.driver_account1 = driver_account1;
                        this.driver_pwd1 = driver_pwd1;
                        this.driver_email1 = driver_email1;
                        this.driver_tel1 = driver_tel1;
                        this.driver_carid1 = driver_carid1;
                }
                public sendPostRunnable(EditText account, EditText pwd, EditText email,
                                EditText tel, EditText carid) {
                        // TODO Auto-generated constructor stub
                }
                public sendPostRunnable(String account_s, String pwd_s, String pwd2_s,
                                String email_s, String tel_s, String carid_s) {
                        // TODO Auto-generated constructor stub
                }
                @Override
                public void run() {
                        String result = sendPostDataToInternet(driver_account1,driver_pwd1,
                                        driver_email1,
                                        driver_tel1,
                                        driver_carid1);

                        myHandler.obtainMessage(REFRESH_DATA, result).sendToTarget();

                }
        }
       
        private String sendPostDataToInternet(String driver_account1,String driver_pwd1,
                        String driver_email1,
                        String driver_tel1,
                        String driver_carid1)
        {

                /* 建立HTTP Post連線 */

                HttpPost httpRequest = new HttpPost(uriAPI);
                /*
                 * Post運作傳送變數必須用NameValuePair[]陣列儲存
                 */
                List<NameValuePair> params = new ArrayList<NameValuePair>();

                //account,pwd,pwd2,email,tel,carid
               
                params.add(new BasicNameValuePair("account",driver_account1));
                params.add(new BasicNameValuePair("pwd",driver_pwd1));
                params.add(new BasicNameValuePair("email",driver_email1));
                params.add(new BasicNameValuePair("tel",driver_tel1));
                params.add(new BasicNameValuePair("carid",driver_carid1));

                try

                {
                        /* 發出HTTP request */

                        httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
                        /* 取得HTTP response */
                        HttpResponse httpResponse = new DefaultHttpClient()
                                        .execute(httpRequest);
                        /* 若狀態碼為200 ok */
                        if (httpResponse.getStatusLine().getStatusCode() == 200)
                        {
                                /* 取出回應字串 */
                                String strResult = EntityUtils.toString(httpResponse
                                                .getEntity());
                                // 回傳回應字串
                                return strResult;
                        }
                } catch (Exception e)
                {
                        e.printStackTrace();
                }
                return null;
        }


最後是PHP端的
<?php
// Dreamweaver的函式庫

if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")

{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? """ . $theValue . """ : "NULL";
      break;   
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? """ . $theValue . """ : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$database_dblink = "new_hitch";
$username_dblink = "hitch";
$password_dblink = "";
// 建立資料庫連線
$dblink = mysql_pconnect("localhost", $username_dblink, $password_dblink) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_query("SET NAMES utf8",$dblink);
mysql_query("SET CHARACTER_SET_CLIENT=utf8",$dblink);
mysql_query("SET CHARACTER_SET_RESULTS=utf8",$dblink);
mysql_select_db($database_dblink, $dblink);

// 宣告utf-8的編碼
header("Content-Type:text/html; charset=utf-8");
// 接收POST/GET的資料

$account=@$_REQUEST["account"];  
$pwd=@$_REQUEST["pwd"];  
$email=@$_REQUEST["email"];  
$tel=@$_REQUEST["tel"];  
$carid=@$_REQUEST["carid"];  





        // 將資料輸入進資料庫
$insertSQL = sprintf("INSERT INTO `driver` (`driver_account`,`driver_pwd`,`driver_email`,`driver_tel`,`driver_carid`) VALUES ("%s","%s","%s","%s","%s");", $account, $pwd, $email, $tel, $carid);
    mysql_query($insertSQL, $dblink) or die(mysql_error());

// 從資料庫撈出來最後一筆資料
$query_rs = "SELECT * FROM `driver` order by log_id desc limit 0,1";
$rs = mysql_query($query_rs, $dblink) or die(mysql_error());
while ($row = mysql_fetch_assoc($rs))
{
        echo "driver_account=".$row["driver_account"]."
"."time=".$row["post_time"];
        //echo "data2=".$row["data2"]."
"."time=".$row["post_time"];
}
?>  



資料參考來源
http://j796160836.pixnet.net/blog/post/30577968-%5Bandroid%5D-%E4%BD%BF%E7%94%A8http%E7%9A%84post%E6%96%B9%E5%BC%8F%E5%92%8C%E7%B6%B2%E9%A0%81%E8%A1%A8%E5%96%AE%E6%BA%9D%E9%80%9A-(%E5%8A%A0

http://givemepass.blogspot.tw/2011/11/alertdialog.html



「用Android 就來APK.TW」,快來加入粉絲吧!
Android 台灣中文網(APK.TW)
收藏收藏 分享分享 分享專題
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

沙發
xjxuvup | 收聽TA | 只看該作者
發表於 2013-11-6 14:11
把錯誤的log訊息貼出來看看
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

板凳
 樓主| darklight | 收聽TA | 只看該作者
發表於 2013-11-6 14:27
xjxuvup 發表於 2013-11-6 14:11
把錯誤的log訊息貼出來看看

用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

地板
whl | 收聽TA | 只看該作者
發表於 2013-11-6 21:59
有用 web browser 試過了嗎?
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

5
 樓主| darklight | 收聽TA | 只看該作者
發表於 2013-11-6 22:23
whl 發表於 2013-11-6 21:59
有用 web browser 試過了嗎?

PHP 那端嗎?

那我也測試過了,按重新整會自己新增一筆資料。

這是正常情況嗎?
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則