首先我们要了解Tomcat,Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选。当配置正确时,Apache 为HTML页面服务,而Tomcat 实际上运行JSP 页面和Servlet。另外,Tomcat和IIS等Web服务器一样,具有处理HTML页面的功能,另外它还是一个Servlet和JSP容器,独立的Servlet容器是Tomcat的默认模式。

意思就是说,下载Tomcat解压后,打开bin目录下的startup.bat,当运行出Server startup in ****ms后,就相当于运行了一个小型服务器,此时,我们就可以通过Activity进行Web通信。想实现Get、Post方法,还必须了解你下载的Webapps中的内容,具体在代码中会展现。

Get、Post在子线程中处理,因为如果在主线程中运行的话,AdroidStudio的一个特点是主线程中如果运行时间过长,运行时会结束运行,此时Get,Post这些费时间的操作要移到子线程中处理,这样可优化程序运行。

还需要注意一点的是,当调试程序是,手机和PC机要处于同一网段,即两者要连在同一个网内,局域网也好,外网也好。建议在PC上开放一个WIFI,手机连上WIFI,至于获得PC机的IP,快捷键Window+R键,输入cmd,在弹出的对话框内输入ipgonfit,可获得PC的IP。值得一说的是,Get,Post等方法基本都是固定程序,我上传了我用的Tomcat,希望有帮助。

import android.os.AsyncTask;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; import com.example.administrator.intent.R; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL; public class WebActivity extends AppCompatActivity {
private TextView tv; //用于展现Get、Post方法的结果
private Button search,search1; //search表示Get方法,search1表示Post方法

@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_web); search= (Button) findViewById(R.id.search); tv= (TextView) findViewById(R.id.tv); search1= (Button) findViewById(R.id.search1); search.setOnClickListener(new View.OnClickListener() { //172.23.72.1:8080表示PC机的IP地址
            @Override
public void onClick(View v) {
String url="http://172.23.72.1:8080/HttpTest/index.jsp?option=getUser&uName=jerehedu";
new MygetJob().execute(url); //MygetJob为自己编写的方法
}
});
search1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("===","xxxxxxxx"); //Log等方法可用于判断程序是否运行此处
                                                                  //也可以用Debug模式来判断
String[] arg=new String[2];
arg[0]="http://172.23.72.1:8080/HttpTest/index.jsp?option=getUser&uName=jerehedu";
arg[1]="option=getUser&uName=jerehedu";
new MyPostJob().execute(arg);
}
});
} public class MyPostJob extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... strings) {
HttpURLConnection con = null;
InputStream is = null;
StringBuilder sbd = new StringBuilder();
try {
URL url = new URL(strings[0]);
con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(5*1000);
con.setReadTimeout(5*1000);
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestProperty("Charset","UTF-8");
con.setRequestProperty("Content-type","application/x-www-form-urlencoded");
//params应该是这样的样式 => option=getUser&uName=jerehedu
String params = strings[1];
OutputStream os = con.getOutputStream();
os.write(params.getBytes());
os.flush();
os.close();
if(con.getResponseCode()==200){
is = con.getInputStream();
int next = 0 ;
byte[] b = new byte[1024];
while ((next = is.read(b))>0){
sbd.append(new String(b,0,next));
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(con!=null){
con.disconnect();
}
}
return sbd.toString();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
tv.setText("POST请求结果:"+s);
}
} /**AsyncTask异步任务类
异步任务类的参数
第一个参数会传到doInbackgrond方法中
第三个参数指定doInbackgrond的返回值
doInbackgrond的返回值会被onPostExecute接收
*/
public class MygetJob extends AsyncTask<String,Void,String>{ //onPreExecute在主线程中执行命令,通常进度条的初始化
@Override
protected void onPreExecute() {
super.onPreExecute();
} //doInBackground在子线程中执行命令
@Override
protected String doInBackground(String... params) {
HttpURLConnection con=null;
InputStream is = null;
StringBuilder sbd=new StringBuilder();
try {
URL url=new URL(params[0]);
con = (HttpURLConnection)url.openConnection();
con.setConnectTimeout(5*1000);
con.setReadTimeout(5*1000);
/* Http响应码
200 成功
404 未找到
500 发生错误
*/
if(con.getResponseCode()==200){
is = con.getInputStream();
int next=0;
byte[] bt = new byte[1024];
while ((next=is.read(bt))>0){
sbd.append(new String(bt,0,next));
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(is!= null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(con!=null){
con.disconnect();
}
}
return sbd.toString();
} //onPostExecute在UI线程中执行
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
tv.setText(s);
}
} }

Web通信中的Get、Post方法的更多相关文章

  1. 【测试方法】Web测试中bug定位基本方法

    知识总结:Web测试中bug定位基本方法 涉及知识点:测试方法 在web测试过程中,经常会遇到页面中内容或数据显示错误,甚至不显示,第一反应就是BUG,没错,确实是BUG.进一步了解这个BUG的问题出 ...

  2. [转]WinForm和WebForm下读取app.config web.config 中邮件配置的方法

    本文转自:http://blog.csdn.net/jinbinhan/article/details/1598386 1. 在WinForm下读取 App.config中的邮件配置语句如下: Con ...

  3. [Flex] IFrame系列 —— 在flex的web应用中嵌入html的方法

    在flex的web应用中,我们往往必须有嵌入html的需求,这时候你会发现IFrame很有用! flex而且可以和html中的JavaScript进行交互,flex可以通过iframe调用到html中 ...

  4. Web测试中定位bug的方法

    在web测试过程中,经常会遇到页面中内容或数据显示错误,甚至不显示,第一反应就是BUG,没错,确实是BUG.进一步了解这个BUG的问题出在那里,是测试人员需要掌握的,可以简单的使用浏览器自带开发者工具 ...

  5. 免费生成二维码接口,可直接嵌入到web项目中,附带嵌入方法,任意颜色二维码,任意大小二维码!

    在线体验连接:http://www.zhaimaojun.top/qrcode/ 你是否在项目中寻找方便而且免费的可以直接嵌入到项目中的二维码生成工具呢?你找到了这里,说明你已经找到了!不要犹豫直接拿 ...

  6. Spring在web应用中获得Bean的方法

    一:使用ApplicationContext获得Bean 首先新建一个类,该类必须实现ApplicationContextAware接口,改接口有一个方法,public void setApplica ...

  7. Java Web项目中解决中文乱码方法总结

    一.了解常识: 1.UTF-8国际编码,GBK中文编码.GBK包含GB2312,即如果通过GB2312编码后可以通过GBK解码,反之可能不成立; 2.web tomcat:默认是ISO8859-1,不 ...

  8. web开发中会话跟踪的方法

    1. 什么是会话 会话是指一个终端用户(服务器)与交互系统(客户端)进行通讯的过程. 2. 什么是会话跟踪 对同一个用户对服务器的连续的请求和接受响应的监视.(将用户与同一用户发出的不同请求之间关联, ...

  9. web开发中会话跟踪的方法有哪些

    会话跟踪就是浏览器和服务器通信 1.cookie 2.session 3.隐藏input 4.url重写 5.ip地址

随机推荐

  1. Unity 截取图片并且显示出来

    Unity 截取图片并且显示出来 近期用到了unity的截图,并且把他显示出来,摸索了很多! 讲解一个东西,android可以存储一些文件在本地,比如配置文件等! 对于不同的系统,方法不一! if ( ...

  2. 【HDOJ】2589 正方形划分

    暴力DFS /* 2589 */ #include <cstdio> #include <cstring> #include <cstdlib> #define M ...

  3. [LeetCode#128]Word Ladder II

    Problem: Given two words (start and end), and a dictionary, find all shortest transformation sequenc ...

  4. activiti集成drools实验

    无代码,无真相. 网上的博客代码,都挺片段的.所以,我想找个现成的demo实验代码. 上github ------------------------------------------------- ...

  5. 通过ComponentName获取相应的Widget

    最近在锁屏上研究,如果预置widget,研究了好久,终于找到方法了,先上代码: private int getAppWidgetFromComName(ComponentName providerCo ...

  6. STL之set、multiset、functor&pair使用方法

    set是一个集合容器,其中包含的元素是唯一的,集合中的元素是按照一定的顺序排列的.元素插入过程是按照排序规则插入,所以不能使用指定位置插入. set采用红黑树变体的数据结构实现,红黑树属于平衡二叉树. ...

  7. codeforces 385C Bear and Prime Numbers 预处理DP

    题目链接:http://codeforces.com/problemset/problem/385/C 题目大意:给定n个数与m个询问区间,问每个询问区间中的所有素数在这n个数中被能整除的次数之和 解 ...

  8. Android四大组件之ContentProvider(二)读取设备上的图片、音频和视频

    Android系统提供了MediaScanner,MediaProvider,MediaStore等接口,通过Content Provider的方式提供给用户.当设备开机或者有SD卡插拔等事件发生时, ...

  9. bzoj1222: [HNOI2001]产品加工

    注意时间都是 <= 5的.. #include<cstdio> #include<cstring> #include<cstdlib> #include< ...

  10. Leo 搭积木

    Leo 搭积木[问题描述]Leo是一个快乐的火星人,总是能和地球上的 OIers玩得很 high.2012 到了, Leo 又被召回火星了,在火星上没人陪他玩了,但是他有好多好多积木,于是他开始搭积木 ...