开发者社区> 问答> 正文

关于httppost = new HttpPost(Uri) 返回值为null! 不知道为何

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.javaregisterclick事件后,会报String f=new NetUitl().register(...)空指针,我debug后,发现原来是http.java文件中httppost = new HttpPost(Uri);空指针。

展开
收起
爵霸 2016-03-18 14:33:44 5440 0
0 条回答
写回答
取消 提交回答
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载