由于初学,网上找了很多交互的文章但是都不全,新手看不懂啊- -!最后在csdn几位大哥的帮助下才搞懂的。。
首先,要知道的是android和web之间的通信没有太大区别,android只是像jsp一样的客户端一样而已,客户端输出流,服务端输入流,处理,再输出流,客户端输入流。就这样。都是通过流的形式。把需要传递的数据封装成对象,然后转换成字符串通过post传递。
我之前走了一个误区,以为服务端接到安卓传来的数据后还要再用另一个类或者servlet传递回指定的安卓端,其实只用一个out.write(c);就可以了。
下面写具体的:
1.建一个安卓工程:
在oncreate里建立一个button的监听,这个不用写了吧。。。
监听中写:
String url = null;
String result = null;
String username = "123456";
ConnNet connNet=new ConnNet();//这个是我另一个工具类
List<NameValuePair> params=new ArrayList<NameValuePair>();//着急,还没有利用json,等以后再试试
params.add(new BasicNameValuePair("username", username));
HttpEntity entity;
try {
entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
HttpPost httpPost=connNet.gethttPost(url);//跳转到辅助类去拿到httpPos
httpPost.setEntity(entity);//发送
//从这里开始就要等servlet端的返回流了
HttpClient client=new DefaultHttpClient();
HttpResponse httpResponse=client.execute(httpPost);//异常
if (httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK)
{
result=EntityUtils.toString(httpResponse.getEntity(), "utf-8");//接收 取值
System.out.println("resu"+result);
}
else {
System.out.println("未能成功连接!");
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
辅助类:
package com.example.wyandroid;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.http.client.methods.HttpPost;
public class ConnNet
{
private static final String URLVAR="http://159.226.180.96:8080/MyTomcat/servlet/myservlet";
//将路径定义为一个常量,修改的时候也好更改
//通过url获取网络连接 connection
public HttpURLConnection getConn(String urlpath)
{
String finalurl=URLVAR+urlpath;
HttpURLConnection connection = null;
try {
URL url=new URL(finalurl);
connection=(HttpURLConnection) url.openConnection();
connection.setDoInput(true); //允许输入流
connection.setDoOutput(true); //允许输出流
connection.setUseCaches(false); //不允许使用缓存
connection.setRequestMethod("POST"); //请求方式
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return connection;
}
public HttpPost gethttPost(String uripath)
{
//HttpPost httpPost=new HttpPost(URLVAR+uripath);
HttpPost httpPost=new HttpPost(URLVAR);
System.out.println(URLVAR+uripath);
return httpPost;
}
}
还有在AndroidManifest.xml里加<uses-permission android:name="android.permission.INTERNET" />否则网络连接抛异常
2.现在看servlet
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class myservlet extends HttpServlet {
public myservlet() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
PrintWriter out=response.getWriter();
String username=request.getParameter("username");
//接到安卓端传来的数据后 判断是否是123456并<span style="font-family: Helvetica, Tahoma, Arial, sans-serif;">out.write不同的值给安卓</span>
if ("123456".equals(username)) //用户名存在
{
String a="t";
out.write(a);
}
else //用户名不存在
{
String c="f";
out.write(c);
}
out.flush();
out.close();
}
public void init() throws ServletException {
// Put your code here
}
}
用debug模式测试安卓端可以发送到tomcat并且接收到tomcat返回的数据流
怎么样,很简单吧~
由于我是菜鸟一只,第一次写博客哈,求不被喷,等以后可以自己加入json数据试试