当前位置: 代码迷 >> Android >> Android 传值出现空格 求诸位大牛们帮帮忙解决一下
  详细解决方案

Android 传值出现空格 求诸位大牛们帮帮忙解决一下

热度:71   发布时间:2016-04-28 03:40:31.0
Android 传值出现空格 求各位大牛们帮帮忙解决一下
代码如下:
package com.example.query;

import com.example.login.LoginActivity;
import com.example.rujia.R;
import com.example.rujia.R.layout;
import com.example.rujia.R.menu;
import com.example.zhuce.ZhuceActivity;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.net.ParseException;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class QueryActivity extends Activity {

  
    //private static final String[] m={"标注单人","标注双人","豪华单人","豪华双人","商务单人","商务双人"};
  //  private TextView view ;
 //   private Spinner spinner;
   // private ArrayAdapter<String> adapter;
    
private EditText psg_add_name;
private EditText psg_add_typeNum;
private EditText etPhone;
private EditText fangjian;   //房间号
private Button modpsg_submitbtn;


 private Button dateBtn = null;  
 private Button timeBtn = null;
    private EditText et=null;
    private final static int DATE_DIALOG = 0;
    private Calendar c = null;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_query);

        psg_add_name = (EditText) findViewById(R.id.psg_add_name);
        psg_add_typeNum = (EditText) findViewById(R.id.psg_add_typeNum);
        etPhone = (EditText) findViewById(R.id.etPhone);
        fangjian = (EditText) findViewById(R.id.fangjian);
        
        modpsg_submitbtn = (Button) findViewById(R.id.modpsg_submitbtn);
        
        et=(EditText)findViewById(R.id.et);
        dateBtn = (Button) findViewById(R.id.dateBtn);       
        
      //跳转传值↓↓↓↓↓↓↓↓         
      Bundle bundle = this.getIntent().getExtras();//跳转传值
     //  int AGE = bundle.getInt("AGE");
       String AGE = bundle.getString("AGE");
        fangjian.setText(AGE+"");   

    
        dateBtn.setOnClickListener(new View.OnClickListener(){ //时间控件的
            public void onClick(View v) {
                showDialog(DATE_DIALOG);
            }
        });
        
        
 modpsg_submitbtn.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View arg0) {
System.out
.println("http://2.mylocal.sinaapp.com/AndroidApp/OnlineBook_DingRoom.php?Action=droom&CategoryNo="
+fangjian.getText().toString()//CategoryNo 房间类型
+"&MemberName="
+psg_add_name.getText().toString() //MemberName 真实姓名
+"&ruzname="
+psg_add_typeNum.getText().toString()  // ruzname 身份证号
+"&photo="
+etPhone.getText().toString()//  photo手机号
        +"&datetimepicker="
        + et.getText().toString());  //datetimepicker 入住时间
       // +"&datetimepicker1="
       // + et.getText().toString()); //datetimepicker1 离开时间 
if(!checkEdit()){
return;
}
// TODO Auto-generated method stub
String httpUrl= "http://2.mylocal.sinaapp.com/AndroidApp/OnlineBook_DingRoom.php?Action=droom&CategoryNo="
+ fangjian.getText().toString()//CategoryNo 房间类型
+ "&MemberName="
+ psg_add_name.getText().toString() //MemberName 真实姓名
+"&ruzname="
+ psg_add_typeNum.getText().toString()  // ruzname 身份证号
+"&photo="
+ etPhone.getText().toString()//  photo手机号
        +"&datetimepicker="
        + et.getText().toString();  //datetimepicker 入住时间
      //  +"&datetimepicker1="
      //  + et.getText().toString(); //datetimepicker1 离开时间 
System.out
.println("http://2.mylocal.sinaapp.com/AndroidApp/OnlineBook_DingRoom.php?Action=droom&CategoryNo="
+ fangjian.getText().toString()//CategoryNo 房间类型
+ "&MemberName="
+ psg_add_name.getText().toString() //MemberName 真实姓名
+"&ruzname="
+ psg_add_typeNum.getText().toString()  // ruzname 身份证号
+"&photo="
+ etPhone.getText().toString()//  photo手机号
        +"&datetimepicker="
        + et.getText().toString());  //datetimepicker 入住时间
       // +"&datetimepicker1="
        //+ et.getText().toString()); //datetimepicker1 离开时间 
      HttpPost httpRequest = new HttpPost(httpUrl);
List<NameValuePair> params=new ArrayList<NameValuePair>();

HttpEntity httpentity = null;
try {
httpentity = new UrlEncodedFormEntity(params,"utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
httpRequest.setEntity(httpentity);
HttpClient httpclient=new DefaultHttpClient();
HttpResponse httpResponse = null;
try {
httpResponse = httpclient.execute(httpRequest);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

if (httpResponse.getStatusLine().getStatusCode() == 200) {
String strResult = null;
try {
strResult = EntityUtils.toString(httpResponse.getEntity());
if (strResult.equals("1")) {
Toast.makeText(QueryActivity.this, "预订成功",
Toast.LENGTH_SHORT).show();

Intent intent = new Intent(QueryActivity.this,YudingActivity.class);
startActivity(intent);
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}

//

else
{
Toast.makeText(QueryActivity.this, "请求错误", Toast.LENGTH_SHORT).show();
}

}

private boolean checkEdit() {
// TODO Auto-generated method stub
                return true;

}
        
        }
        );
        
        
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.query, menu);
return true;
}


//调用手机系统中自带的时间选择器
@Override
    protected Dialog onCreateDialog(int id) {
        Dialog dialog = null;
        switch (id) {
        case DATE_DIALOG:
            c = Calendar.getInstance();
            dialog = new DatePickerDialog(
                this,
                new DatePickerDialog.OnDateSetListener() {
                    public void onDateSet(DatePicker dp, int year,int month, int dayOfMonth) {
                        et.setText(year + "年" + (month+1) + "月" + dayOfMonth + "日");
                    }
                }, 
                c.get(Calendar.YEAR), // 传入年份
                c.get(Calendar.MONTH), // 传入月份
                c.get(Calendar.DAY_OF_MONTH) // 传入天数
            );
 
        }
        return dialog;
    }


private boolean checkEdit(){
if(psg_add_name.getText().toString().trim().equals("")){
Toast.makeText(QueryActivity.this, "姓名不能为空", Toast.LENGTH_SHORT).show();
}else if(psg_add_typeNum.getText().toString().trim().equals("")){
Toast.makeText(QueryActivity.this, "身份证号码不能为空", Toast.LENGTH_SHORT).show();
}else if(fangjian.getText().toString().trim().equals("")){
Toast.makeText(QueryActivity.this, "房间类型不能", Toast.LENGTH_SHORT).show();
}else if(etPhone.getText().toString().trim().equals("")){
Toast.makeText(QueryActivity.this, "手机号码不能为空", Toast.LENGTH_SHORT).show();
}else if(et.getText().toString().trim().equals("")){
Toast.makeText(QueryActivity.this, "时间不能空", Toast.LENGTH_SHORT).show();
}
else{
return true;
}
return false;
}



}



 代码和错误如上,值传的时候出现了空格,想求各位大牛教教怎么解决,将空值去掉

------解决思路----------------------
将空格trim掉
------解决思路----------------------
引用:
将空格trim掉

+1 string的方法trim();
------解决思路----------------------
replace("\\s","");替换掉也行
  相关解决方案