android发送post请求出现问题

我用 android 发送post请求时遇到一点问题,我将代码贴上,望大家能帮我解决哈问题啊!



private String baseUrl = "http://localhost:8080/KUPA/ClientAction!adduser"; 
    private HttpResponse response = null;//响应对象
    private HttpPost posts = null;//响应对象
//连接服务器,发送post请求
public void givePosts(){
            NameValuePair Pair = new BasicNameValuePair("name",nameedittext.getText().toString());//键值对
            NameValuePair Pair1 = new BasicNameValuePair("sex",rbuttonsex.getText().toString());//键值对
            NameValuePair Pair2 = new BasicNameValuePair("birthday",birthedittext.getText().toString());//键值对
            //然后将键值对放到列表里(类似于形成数组)
            List<NameValuePair> Pairslist = new ArrayList<NameValuePair>();
            Pairslist.add(Pair);//将键值对放入到列表中
            Pairslist.add(Pair1);//将键值对放入到列表中
            Pairslist.add(Pair2);//将键值对放入到列表中
            posts=new HttpPost();
                try {
                //生成一个post请求对象
                    posts= new HttpPost(baseUrl);
                //发送Http Request 
                    posts.setEntity(new UrlEncodedFormEntity(Pairslist,HTTP.UTF_8));
//生成一个http客户端对象
HttpClient httpClient = new DefaultHttpClient();//发送请求
response = httpClient.execute(posts);//接收响应,debug调试这一句始终执行不了,系统也没有抛出异常,也加了允许访问网络的权限 //如果返回状态为200,获得返回的结果   
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String str = EntityUtils.toString(response.getEntity());
if (str.trim().equals("success")) {
toast = Toast.makeText(getApplicationContext(),
"添加信息成功!", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
} else {
toast = Toast.makeText(getApplicationContext(),
"添加信息失败!", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}
} catch (Exception e) {
// TODO: handle exception
}
}



有知道错误的请回复下,我的QQ:475507815

你可能感兴趣的:(android)