RT
http.java
public class Http {
static HttpEntity entity; //HttpEntity是可以同Http消息进行接受或发送的实体,可以是流或者字符串
static HttpResponse response; //HttpResponse是一个Http连接响应,可以通过它来获得一些响应的信息
static HttpPost httppost;
static HttpClient httpclient;
static Context context;
public String strResult;
public String Get(String Uri) {
return null;
}
public String Post(final String Uri, final List<NameValuePair> params) {
new Thread() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
//客户端数据打包
httpclient = new DefaultHttpClient();//新建httpclient对象, 默认的Http客户端,可以用它来创建HTTP连接
httppost = new HttpPost(Uri); //新建httppost对象, 连接url
httppost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
//客户端发送数据请求,并得到服务器返回数据
response = httpclient.execute(httppost); //获取HttpResponse实例
entity = response.getEntity(); // 得到内容
if (response.getStatusLine().getStatusCode() == 200) {
// 取出回应字串
strResult = EntityUtils.toString(response.getEntity());
}
} catch (Exception e) {
e.printStackTrace();// 报错识别
}
super.run();
}
}.start();
return strResult;
}
}
NetUtil。java
public class NetUitl {
public String register(String Username, String Password,String Idcard,String Name,String Sex,int Tel) {
String Uri = "http://127.0.0.1:8080/Client/Register";
String tel=String.valueOf(Tel);
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("Username", Username));
params.add(new BasicNameValuePair("Password", Password));
params.add(new BasicNameValuePair("Idcard", Idcard));
params.add(new BasicNameValuePair("Sex", Sex));
params.add(new BasicNameValuePair("Realname", Name));
params.add(new BasicNameValuePair("Tel", tel));
return new Http().Post(Uri, params);
}
register。java
public class register extends Activity {
EditText username,password,idcard,name,sex,time,tel;
Button register,login;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
username=(EditText) findViewById(R.id.zhuceusername);
password=(EditText) findViewById(R.id.zhucepassword);
idcard=(EditText) findViewById(R.id.zhuceidcard);
name=(EditText) findViewById(R.id.zhucename);
sex=(EditText) findViewById(R.id.zhucesex);
tel=(EditText) findViewById(R.id.zhucetel);
register=(Button) findViewById(R.id.zhuce);
login=(Button) findViewById(R.id.denglu);
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
register.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try{
if(!username.getText().toString().equals("")&&!password.getText().toString().equals("")&&
!idcard.getText().toString().equals("")&&!name.getText().toString().equals("")&&
!sex.getText().toString().equals("")&&!tel.getText().toString().equals("")){
String f=new NetUitl().register(username.getText().toString(),password.getText().toString(),
idcard.getText().toString(), name.getText().toString(), sex.getText().toString(), Integer.valueOf(tel.getText().toString()));
if(f.equals("true")){
Intent intent=new Intent(register.this,zhucesuccess.class);
startActivity(intent);
finish();}
}else{
Toast.makeText(register.this, "内容不能为空!", Toast.LENGTH_SHORT).show();}
}catch(Exception e){
Toast.makeText(register.this, "注册失败,服务器连接错误", Toast.LENGTH_SHORT).show();
}}});
}}).start();
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent=new Intent(register.this,Login.class);
startActivity(intent);
}
});
}
}
就是注册不成功
我补充一下,我执行register.java
的register
的click
事件后,会报String f=new NetUitl().register(...)
空指针,我debug
后,发现原来是http.java
文件中httppost = new HttpPost(Uri);
空指针。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。