Android的提交数据到服务器
16lz
2021-01-25
在Android中提交数据到服务器和平时的浏览器差不多分为三步:
第一步:打开一个浏览器,
第二部:输入一个网址;
第三部:敲回车。
Get实现代码例:
//打开一个浏览器HttpClient client = new DefaultHttpClient();String path = "http://192.168.1.100:8080/web/LoginServlet?username=" + username + "&password=" + password;//输入网址HttpGet httpGet = new HttpGet(path);//敲回车HttpResponse response = client.execute(httpGet);int code = response.getStatusLline().getStatusCode();if(code == 200){ InputStream is = response.getEntity.getContent(); String text = StreamTools.readInputStream(is);}
Post实现代码例:
//打开一个浏览器HttpClient client = new DefaultHtttClient();//输入网址String path = "http://192.168.1.100:8080/web/LoginServlet";HttpPost httpPost = new HttpPost(path);//指定要提交的数据实体List<NameValuePair> parameters = new ArrayList<NameValuePair>();parameters.add(new BasicNameValuePair("username", username));parameters.add(new BasicNameValuePair("password", password));httpPost.setEntity(new UrlEncodeFormEntiry(parameters, "UTF-8"));//敲回车HttpResponse response = client.execute(httpPost);int code = response.getStatusLine().getStatusCode();
更多相关文章
- 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
- 浅谈Android之SurfaceFlinger相关介绍(二)
- 向sd写文件时权限
- Eclipse+CDT+GDB调试android NDK程序
- android ctrl + 左键(鼠标左键)直接打开xml文件
- android j使用JNI实现ava语言调用C语言
- android 手机内存SD卡和cpu等信息的获取
- PianoView-添加一行代码使用[最美应用]的酷炫控件!
- android proguard混淆apk问题