android访问web程序,Android手机访问web服务器(post请求)

一:客户端—服务器连接操作类(HttpUtil)

package com.example.userdatatoweb;

import java.util.ArrayList;

import java.util.List;

import java.util.Map;

import java.util.Map.Entry;

import java.util.Set;

import org.apache.http.HttpResponse;

import org.apache.http.NameValuePair;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.protocol.HTTP;

import org.apache.http.util.EntityUtils;

/**

* Android客户端连接服务器操作类

* */

public class HttpUtil {

public static String serverPath="222.23.162.221:8080";

//服务器的ip地址以及参数

public static String contextPath="/prjMyschoolmate";

//服务器虚拟目录(web项目名),研究web服务器上下文路径

public static String loginUrl="http://"+serverPath+contextPath+"/servlet/LoginServlet";

//更新学生信息

/**

* 发送请求,并获取相应,用post访问服务器

* @param url 请求地址

* @param map 请求参数

* @throws Exception

* */

public static String sendRequest(String url,Map map) throws Exception{

String msg="";

String urlstr=url;

//准备post请求对象

HttpPost request=new HttpPost(urlstr);

List params=new ArrayList();

//这个内容:把map中的键值对,存放到params对象中,该对象是一个List集合,内容为NameValuePair对象

if(map!=null){

Set> set=map.entrySet();

for(Entry entry:set){

params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));

}

}

try {

//将参数按要求保存请求中

request.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));

HttpResponse response=new DefaultHttpClient().execute(request);

//判断登录是否成功

if(response.getStatusLine().getStatusCode()==200){

//将返回的数据变string保存msg

msg=EntityUtils.toString(response.getEntity());

}else if(response.getStatusLine().getStatusCode()==404){

msg="404-服务器不存在";

}else if(response.getStatusLine().getStatusCode()==500){

msg="505-抱歉,登录码编译编译错误";}else{

msg="登录失败";}

} catch (Exception e) {

e.printStackTrace();

throw new Exception("登录失败");

}

return msg;

}

}

三:向服务器添加用户类(AddUserActivity)

package com.example.userdatatoweb;

import java.util.HashMap;

import java.util.Map;

import android.os.Bundle;

import android.app.Activity;

import android.content.Intent;

import android.util.Log;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.EditText;

import android.support.v4.app.NavUtils;

public class AddUserActivity extends Activity {

EditText txtName;

EditText txtAge;

EditText txtTel;

EditText txtAddress;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.adduser);

//获取界面控件

txtName=(EditText) findViewById(R.id.etName);

txtAge=(EditText) findViewById(R.id.etAge);

txtTel=(EditText) findViewById(R.id.etTel);

txtAddress=(EditText) findViewById(R.id.etAddress);

}    /**

* 保存用户

* */

public void saveUser(View view){     //获取界面数据

String name=txtName.getText().toString();

String age=txtAge.getText().toString();

String tel=txtTel.getText().toString();

String address=txtAddress.getText().toString();     //map集合封装界面数据

Map map=new HashMap();

map.put("name", name);

map.put("age", age);

map.put("tel", tel);

map.put("address", address);

try {

String result=HttpUtil.sendRequest(HttpUtil.addUser, map);   //跳转用户列表页

Intent intent=new Intent();

intent.setClass(this, UserListActivity.class);

startActivity(intent);

this.finish();

} catch (Exception e) {

Log.i("Web", name+"页面跳转失败");

e.printStackTrace();

}

}

注:添加界面:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:stretchColumns="1" >

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/useradd"

android:gravity="center"

android:textSize="18sp" />

android:layout_width="fill_parent"

android:layout_height="@dimen/useraddline"

android:background="#fff"/>

android:id="@+id/tableRow2"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/username"/>

android:id="@+id/etName"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text=""/>

android:id="@+id/tableRow3"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/userage"/>

android:id="@+id/etAge"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text=""

android:numeric="integer"/>

android:id="@+id/tableRow4"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/usertel"/>

android:id="@+id/etTel"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text=""

android:numeric="decimal"/>

android:id="@+id/tableRow5"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/useraddress"/>

android:id="@+id/etAddress"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text=""/>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/save"

android:onClick="saveUser" />

简单web服务器:

0818b9ca8b590ca3270a3433284dd417.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值