Android程序与服务进行数据交互
交互的方式有,GET、POST、AsyncHttpClient(异步网络客户提交数据使用的第三方),常用的有,POST、AsyncHttpClient。
因为,是通过网络,向服务器提交数据,所有,必须给,Android程序,添加网络使用权限,三种方式都,必须使用!
<!--添加,网络权限-->
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
GET方式,向服务器,提交数据
- 步骤
- 因为是,通过网络,向服务器提交数据,此为耗时操作,故此需要,使用线程,然后是,通过URL类向服务器,提交数据。
//点击事件
public void onclick(View view){
//服务器的网络,地址
String path="http://172.20.10.3:8080/server/server.do";
//需要,传递的值
String sname=editTextone.getText().toString();
String pwrodd=editTexttwo.getText().toString();
//通过,异步任务类,来实现,具体操作。
new Myasyctask().execute(sname,pwrodd,path);
}
//异步任务类
class Myasyctask extends AsyncTask{
@Override
protected Object doInBackground(Object[] params) {
//取到,传递过来的值
String sname=params[0].toString();
String spwd=params[1].toString();
String pth=params[2].toString();
try {
//建立URL对象,把数据,通过地址栏传参的方式,填入。
URL url=new URL(pth+"?name="+sname+"&pwd="+spwd+" ");
//取到,网络连接
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
//设置,请求的类型 get
connection.setRequestMethod("GET");
//连接时间 为 5秒,此处单位:毫秒。 如果,网络差,超过,这个时限,将不执行剩下的操作了
connection.setConnectTimeout(5000);
//HTTP状态码 200 服务器响应正常
if(connection.getResponseCode()==200){
//取到,服务器的输入流
InputStream inputStream=connection.getInputStream();
BufferedReader bufferedInputStream=new BufferedReader(new InputStreamReader(inputStream));
//吧取到的数据,发送到 类中的onPostExecute(Object o)(专门,更新UI之类的) 方法中。
return bufferedInputStream.readLine();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override //更新UI 的方法
protected void onPostExecute(Object o) {
super.onPostExecute(o);
//值变量o,就是 doInBackground(Object[] params) 返回,过来的值。
//Log.i("test",(String)o+"状态码");
// Toast.makeText(MainActivity.this, ""+o.toString(), Toast.LENGTH_SHORT).show();
}
}
POST方式,向服务器,提交数据
与GET方式的区别,就是增加了,请求头,而且,值也是,通过网络写入,传递。查看POST 请求的方法
// 使用post 方式,向服务器,提交数据
class MyAsycTaskTwo extends AsyncTask{
@Override
protected Object doInBackground(Object[] params) {
String sname=params[0].toString();
String spwd=params[1].toString();
//服务器的网络地址
String pth=params[2].toString();
try {
//建立,url对象,参数为,服务器的地址
URL url=new URL(pth);
//取得,网络,连接
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
//设定,请求,的方式
connection.setRequestMethod("POST");
//设定,连接超时,时间
connection.setConnectTimeout(4000);
//建立需要传递过去,值的变量
//这里的 name pwd 是传递参数的键
String siteValue="name="+sname+"&pwd="+spwd;
//与get 方式,区别的的地方
//添加,请求头
//参数一:这个键是,固定,如此的 参数二: 要传递值的长度
connection.setRequestProperty("Content-Length",siteValue.length()+"");
//这二个,参数都是固定的,在浏览器中可以查看到
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
//设定,连接的输出,为允许
connection.setDoOutput(true);
//取到,连接的,输出的对象,使用它,来进行,向服务器的传递值
OutputStream out=connection.getOutputStream();
//把需要传递过去的 值 对象,转换成字节数组,然后,写出
out.write(siteValue.getBytes());
//刷新
out.flush();
//关闭
out.close();
//当 服务器,正常响应
if(connection.getResponseCode()==200){
//取到,连接字节输入流, 接受,服务器的返回数据
InputStream inputStream=connection.getInputStream();
//根据,需求,转换
BufferedReader bufferedInputStream=new BufferedReader(new InputStreamReader(inputStream));
//把,服务器,返回过来的值,传递给onPostExecute(Object o) 方法
return bufferedInputStream.readLine();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
}
}
使用AsyncHttpClient,向服务器,提交数据
- 先复制,jar包进入到,项目中的libs 文件件内,再对,刚加入的jar包,右键,选择Add As Libray
public void loginAsyncHttpClient(View view){
String uname=et_main_uname.getText().toString();
String upwd=et_main_upwd.getText().toString();
//服务器的,地址
String path="http://193.168.2.78:6080/Server/login.do";
AsyncHttpClient ahc=new AsyncHttpClient();
RequestParams params=new RequestParams();
params.put("uname",uname);
params.put("upwd",upwd);
ahc.post(this,path,params,new TextHttpResponseHandler(){
@Override
public void onFailure(int statusCode, Header[] headers, String responseBody, Throwable error) {
super.onFailure(statusCode, headers, responseBody, error);
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseBody) {
super.onSuccess(statusCode, headers, responseBody);
Toast.makeText(MainActivity.this, ""+responseBody, Toast.LENGTH_SHORT).show();
}
});
}