Android HTTPPost List <namevaluepair>发布到php?</namevaluepair>

时间:2012-07-25 03:55:18

标签: php android http-post http-get

我一直在寻找几个小时,我仍然不清楚HTTPPost方法。我有这样的代码...

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 个答案:

答案 0 :(得分: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;
}