2012-07-25 114 views
0

我一直在搜索小时,但我仍然不清楚HTTPPost方法。我有这样的代码...Android HTTPPost List <NameValuePair> post to php?

httpclient = new DefaultHttpClient(); 
httppost = new HttpPost(url); 
// Add your data 
Log.i("ACTIVITY","PostInfo"); 
List<NameValuePair> pairs = new ArrayList<NameValuePair>(); 
pairs.add(new BasicNameValuePair("email", stringeditemail)); 
httppost.setEntity(new UrlEncodedFormEntity(pairs)); 

这是应该将信息发布到给定的PHP从指定的网站? 如果是这样,我做错了,它不是发布?

这是我的PHP

<?php 


require('include/config.php'); 
require('include/function.php'); 
require('classes/captcha.class.php'); 
require('language/' .$_SESSION['language']. '/signup.lang.php'); 

if ($config['user_registrations'] == 0) { 
$msg = $lang['signup.registration_disabled']; 
session_write_close(); 
header('Location: index.php?msg=' .$msg); 
die(); 
} 

$email  = NULL; 
if (isset($_REQUEST['action_signup']) && $_REQUEST['action_signup'] != '') 
{ 
$email  = $filterObj->process(trim($_POST['email'])); 

if($email == '') 
    $err = $lang['signup.email_empty']; 
elseif (!check_email($email)) 
    $err = $lang['signup.email_invalid']; 
elseif (check_field_exists($email, 'email', 'signup') == 1) 
    $err = $lang['signup.email_exists']; 

$_REQUEST['pack_id'] == '') 
    $err = $lang['signup.select_package']; 

    if ($err == '') { 
    $email  = mysql_real_escape_string($email); 

    $sql  = "insert into signup set email='" .$email. "'; 
    $conn->execute($sql); 


    if($config['enable_package'] == 'yes') { 
     $pack_id = mysql_real_escape_string($_REQUEST['pack_id']); 
     $sql = "select * from package where pack_id='" .$pack_id. "'"; 
     $rs = $conn->execute($sql); 

     } else { 
      $sql = "update signup set acount_status='Inactive' where  UID='" .$userid. "' limit 1"; 
      $conn->execute($sql); 
      session_write_close(); 
      header("Location: pack_ops.php?pack=$_REQUEST[pack_id]&uid=".base64_encode($userid)); 
      die(); 
     } 
    } 

    $sql = "INSERT INTO users_online (UID, online) VALUES (" .$userid. ", " .time(). ")"; 
    $conn->execute($sql); 

      $_SESSION['EMAIL']  = $_REQUEST['email']; 


    $ran=time().rand(1,99999999); 
    $sql="update verify as v, signup as s set v.vcode='" .$ran. "', s.emailverified='no' WHERE v.UID=s.UID and v.UID='" .$userid. "'"; 
    $conn->execute($sql); 
    STemplate::assign('vcode',$ran); 

    $to   = $_SESSION['EMAIL']; 
    $name  = $config['site_name']; 
    $from  = $config['admin_email']; 
    $rs   = $conn->execute("select * from emailinfo where email_id='verify_email'"); 
    $subj  = $rs->fields['email_subject']; 
    $email_path  = $rs->fields['email_path']; 
    $mailbody = STemplate::fetch($email_path); 
    mailing($to,$name,$from,$subj,$mailbody); 

    $_SESSION['verification_sent'] = $lang['signup.verification_sent']; 

    $redirect = (isset($_SESSION['redirect']) && $_SESSION['redirect'] != '') ? $_SESSION['redirect'] : $config['BASE_URL']; 
    $_SESSION['redirect'] = NULL; 

    session_write_close(); 
    header('Location: ' .$redirect); 
    die(); 
} 
    } 

if ($config['enable_package'] == 'yes') { 
    $sql = "select * from package where status = 'Active' order by price desc"; 
    $rs = $conn->execute($sql); 
    STemplate::assign('package', $rs->getrows()); 
} 

STemplate::assign('err',$err); 
STemplate::assign('msg',$msg); 
STemplate::assign('head_bottom',"homelinks.tpl"); 
STemplate::assign('username', $username); 
STemplate::assign('email', $email); 
STemplate::display('head1.tpl'); 
STemplate::display('err_msg.tpl'); 
STemplate::display('signup.tpl'); 
STemplate::display('footer.tpl'); 
STemplate::gzip_encode(); 
?> 
+1

你叫'HttpClient.exeucute(httpPost)',并得到了'HttpResponse'? – neevek 2012-07-25 04:09:47

回答

1

尝试这样,你可以使用:HttpClient.exeucute(httpPost)并得到 的HttpResponse

这项工作对我来说完美。

DefaultHttpClient client = new DefaultHttpClient(); 

    HttpPost post = new HttpPost(url); 

    List<NameValuePair> params = new ArrayList<NameValuePair>(); 

    pairs.add(new BasicNameValuePair("email", stringeditemail)); 

    UrlEncodedFormEntity formEntity = null; 
    try { 
     formEntity = new UrlEncodedFormEntity(params); 

    } catch (UnsupportedEncodingException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 

    post.setEntity(formEntity); 

    try { 

     HttpResponse response = client.execute(post); 
     int statusCode = response.getStatusLine().getStatusCode(); 
     if (statusCode == HttpStatus.SC_OK) { 
      HttpEntity entity = response.getEntity(); 
      InputStream is = entity.getContent(); 
      return iStream_to_String(is); 
     } else { 
      return "Hello This is status ==> :" 
        + String.valueOf(statusCode); 
     } 
    } catch (UnsupportedEncodingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    return null; 

} 

,并为转换方法StringBuilder的字符串

public static String iStream_to_String(InputStream is1) { 
    BufferedReader rd = new BufferedReader(new InputStreamReader(is1), 4096); 
    String line; 
    StringBuilder sb = new StringBuilder(); 
    try { 
     while ((line = rd.readLine()) != null) { 
      sb.append(line); 
     } 
     rd.close(); 

    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    String contentOfMyInputStream = sb.toString(); 
    return contentOfMyInputStream; 
} 
+0

会以这种方式去.. + 1 – 2012-07-25 04:40:38

+0

我收到回复,但没有发布。代码是否可能停在'http:// www.website.com'而不是'http:// www.website.com/requested.php'? – Christian 2012-07-26 02:05:58

+0

网址不是问题,我只是缺少一些字符串 – Christian 2012-07-26 02:53:09