在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();










更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. 浅谈Android之SurfaceFlinger相关介绍(二)
  3. 向sd写文件时权限
  4. Eclipse+CDT+GDB调试android NDK程序
  5. android ctrl + 左键(鼠标左键)直接打开xml文件
  6. android j使用JNI实现ava语言调用C语言
  7. android 手机内存SD卡和cpu等信息的获取
  8. PianoView-添加一行代码使用[最美应用]的酷炫控件!
  9. android proguard混淆apk问题

随机推荐

  1. Android常见错误处理
  2. Android Studio 导入 GreenDao
  3. 使用PHP开发Android应用程序技术介绍
  4. Android:Designing for Performance
  5. Android(一)Android Eclipse环境搭建
  6. Android_RelativeLayout属性大全
  7. Error:(1, 0) The android gradle plugin
  8. 使用AudioTrack播放PCM音频数据(android)
  9. android 过渡动画研究
  10. Android SAX 解析XML的Demo