nwwyjf 2016-01-05 08:16 采纳率: 0%
浏览 2644

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

  • 写回答

6条回答 默认 最新

  • tony4geek 2016-01-05 08:31
    关注

    你调试你的 public String Post(final String Uri, final List params) { 这个 里面 看看什么原因。

    评论

报告相同问题?

悬赏问题

  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档