登录界面 Andriod简单http post请求 六

get方法提交Http请求时,,会在URL中显示提交的参数,造成一定的不安全性,且提交的参数的长度受到URL长度限制。

下面采用post请求提交,同一个项目。

在前文基础上,只需改动HttpUtils包,代码如下:

public class httpUtils {private static final String TAG="httputils";static String pathString=null;public httpUtils() {// TODO Auto-generated constructor stub};public static void sendPost(String username,String password){Log.d(TAG, username);Log.d(TAG, password);//post提交采用直接在post参数Entity中设置 list<NameValuePair>形式//NameValuePair为一个抽象类型,需要实际类型pathString=":8080/Myweb/Webdo";Log.d(TAG, pathString);//创建httpclientHttpClient client=new DefaultHttpClient();//创建GET请求//HttpGet get=new HttpGet(pathString);HttpPost post=new HttpPost(pathString);List<NameValuePair>list=new ArrayList<NameValuePair>();list.add(new BasicNameValuePair("username", username));list.add(new BasicNameValuePair("password", password));try {//设置Entitypost.setEntity(new UrlEncodedFormEntity(list,HTTP.UTF_8));} catch (UnsupportedEncodingException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}try {//返回结果HttpResponse response=client.execute(post);//请求成功if(response.getStatusLine().getStatusCode()==200){Log.d(TAG, "sucess!!");}} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}};}

后面几篇博文将对XML,json解析进行分析

iOS端请求时,与系列五中参数设置类似,只需更改为post提交即可,不再赘述。

更重要的是心理上的完全自由和放松,

登录界面 Andriod简单http post请求 六

相关文章:

你感兴趣的文章:

标签云: