关闭

Android——JavaWeb servlet通信

标签: Android网络编程
32人阅读 评论(0) 收藏 举报
分类:

Android-Client :

public class servlett extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shape);
findViewById(R.id.tv).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new Thread() {
public void run() {
try {
URL url = new URL("http://192.168.1.45:8080/day07_02_ServletContext/servlet/ServletContextDemo3?z=zz&t=tt");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 设置请求方法,默认是GET
connection.setRequestMethod("GET");
// 设置字符集
connection.setRequestProperty("Charset", "UTF-8");
// 设置文件类型
connection.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
// 设置请求参数,可通过Servlet的getHeader()获取
connection.setRequestProperty("Cookie", "AppName=" + URLEncoder.encode("你好", "UTF-8"));
// 设置自定义参数
if(connection.getResponseCode() == 200){
InputStream is = connection.getInputStream();
//                    result = StringStreamUtil.inputStreamToString(is);

Message msg = Message.obtain();
msg.what = 0;
//                    getHandler.sendMessage(msg);
}
} catch (Exception e) {
}
};
}.start();
}
});

}

注意权限: <uses-permission android:name="android.permission.INTERNET" /> 

Server:

Object obj1;
obj1 = request.getParameter("z");
System.out.println(obj1);
obj1 = request.getParameter("t");
System.out.println(obj1);




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1141次
    • 积分:193
    • 等级:
    • 排名:千里之外
    • 原创:19篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条