正文
慢慢地,二维码实现签到将要落幕了。下篇文章出二维码实现签到
这次
我们实现 javaweb http json 交互 in action
题目很长,但我想让你们看下,给我点意见。
开始吧
实战
本次以经典的登录作为案例。登录做的好也是经典。 服务端 和 app端,服务端简略,app端详细介绍...
服务端
资料:
《spring》
@ResponseBody
将内容或对象作为 HTTP 响应正文返回,并调用适合HttpMessageConverter的Adapter转换对象,写入输出流。
- GET模式下,这里使用了@PathVariable绑定输入参数,非常适合Restful风格。因为隐藏了参数与路径的关系,可以提升网站的安全性,静态化页面,降低恶意攻击风险。
- POST模式下,使用@RequestBody绑定请求对象,Spring会帮你进行协议转换,将Json、Xml协议转换成你需要的对象。
- @ResponseBody可以标注任何对象,由Srping完成对象——协议的转换。
代码例子:
@RequestMapping(value = "/login")
public @ResponseBody Results login(@PathVariable String username, @PathVariable String password) throws IOException
{
System.out.println("userName:"+username);
System.out.println("password:"+password);
Results result = new Results();
int resultState = userLoginService.checkUserLoginByPhone(username,password);
if(resultState == 1)
{
result.setResults(1);
System.out.println("... phone success!!");
}
else if(resultState == 0)
{
result.setResults(0);
System.out.println("... phone fail!!");
}
return result;
}
Results 类很简单
package sedion.jeffli.wmuitp.web.webservice;
public class Results
{
private int results;
public int getResults()
{
return results;
}
public void setResults(int results)
{
this.results = results;
}
}
手机端
资料 :
1.android + java
2.http
3.json
结构:
①itp包下,是程序的主入口,界面很简单。
package sedion.jeffli.itp;
import sedion.jeffli.util.SystemUtil;
import sedion.jeffli.util.UIHelper;
import sedion.wq.itp.webservice.WebService;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity
{
/**帐号输入框**/
private EditText accountEditText;
/**密码输入框**/
private EditText passwordEditText;
/**登陆按钮**/
private Button loginButton;
/**登陆后台任务**/
private LoginAsyncTask loginAsyncTask;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//控件初始实例化
accountEditText = (EditText) findViewById(R.id.activity_login_account);
passwordEditText = (EditText) findViewById(R.id.activity_login_password);
loginButton = (Button) findViewById(R.id.activity_login_btn_login);
//控件事件绑定
loginButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
String userName = accountEditText.getText().toString();
String userPassword = passwordEditText.getText().toString();
String[] params = new String[] { userName, userPassword };
loginAsyncTask = new LoginAsyncTask();
loginAsyncTask.execute(params);
loginButton.setEnabled(false);
}
});
}
class LoginAsyncTask extends AsyncTask<String, Integer, Integer>
{
@Override
protected Integer doInBackground(String... params)
{
int resultId = WebService.login(params[0], params[1]);
Log.e("resultid", resultId+"");
return resultId;
}
@Override
protected void onPostExecute(Integer result)
{
if (loginAsyncTask!=null)
{
loginButton.setEnabled(true);
Log.e("result:", result+"");
//根据不同情况处理
if (result == -4)
{
UIHelper.ToastMessage(MainActivity.this, "网络不可用");
}
else if (result == 0 )
{
UIHelper.ToastMessage(MainActivity.this, "用户名或密码错误");
}
else if (result == 1)
{
UIHelper.ToastMessage(MainActivity.this, "登录成功");
}
else
{
UIHelper.ToastMessage(MainActivity.this, "...");
}
}
}
@Override
protected void onCancelled()
{
loginAsyncTask = null;
loginButton.setEnabled(true);
}
}
}
#LoginAsyncTask 里面是程序的重要部分
你会看到:
②webservice 工具类 ()
HttpClient.java
package sedion.wq.itp.webservice;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class HttpClient
{
/**连接超时**/
private static final String TIME_OUT = "{\"results\":-2}";
/** User-Agent 用户识别 **/
private static final String User_Agent = "...";
/**
* 使用post请求获取数据
* @param uriAPI 网址
* @param params 请求参数
* @return
* @throws JSONException
*/
private static JSONObject post(String uriAPI, List<NameValuePair> params)
{
org.apache.http.client.HttpClient httpClient = new DefaultHttpClient();
JSONObject result = null;
try
{
// 使用post方式
HttpPost httpRequest = new HttpPost(uriAPI);
if (params !=null && params.size()>0)
{
//设置请求参数和编码格式
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
}
//设置请求用户识别
//httpRequest.addHeader("User-Agent", User_Agent);
//设置请求超时时间为5s
httpRequest.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,5000);
//读取超时时间
httpRequest.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
//发送请求并获取响应
HttpResponse httpResponse = httpClient.execute(httpRequest);
//根据不同的请求结果代码进行处理数据
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
//判断请求成功并返回数据
//解析返回的结果,将返回的信息整成字符串
String strResult = EntityUtils.toString(httpResponse.getEntity());
//转化成json对象,便于操作,json对象是类似于hashmap
result = new JSONObject(strResult);
System.out.println("JSONObject -> result:"+result);
//如果返回的数据时空
if (result.optString("results").equals("null"))
{
result = null;
}
}
else
{
//请求失败
Log.e("请求失败", String.valueOf(httpResponse.getStatusLine().getStatusCode()));
result = null;
}
}
catch(ConnectTimeoutException cException)
{
try
{
result = new JSONObject(TIME_OUT);
}
catch (Exception e)
{
Log.e("webservice json 转化错误", e.toString());
}
}
catch (Exception e)
{
Log.e("post Error", e.toString());
e.printStackTrace();
result = null;
}
finally
{
try
{
httpClient.getConnectionManager().shutdown();
}
catch (Exception e2)
{
Log.e("关闭连接失败", e2.toString());
}
}
return result;
}
/**
* 使用post请求获取数据
* @param uriAPI 网址
* @param params 请求参数
* @return
*/
public static JSONObject post(String uri, String method, List<NameValuePair> params)
{
return post(uri + method, params);
}
/**
* 使用get请求获取数据
* @param uriAPI 网址
* @return
*/
public static JSONObject get(String uriAPI,HttpParams params)
{
try
{
//实例化get请求
HttpGet httpRequest = new HttpGet(uriAPI);
if (params !=null)
{
//设置参数
httpRequest.setParams(params);
}
//执行
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == 200)
{
String strResult = EntityUtils.toString(httpResponse.getEntity());
JSONObject result = new JSONObject(strResult);
System.out.println("result:"+result);
return result;
}
else
{
return null;
}
}
catch (Exception e)
{
Log.e("get Error", e.toString());
return null;
}
}
/** post 访问
* @param url
* @param reqEntity
* @return
*/
public static JSONObject post(String url ,MultipartEntity reqEntity )
{
JSONObject result = null;
HttpParams parms = new BasicHttpParams();
parms.setParameter("charset", HTTP.UTF_8);
org.apache.http.client.HttpClient client = new DefaultHttpClient(parms);
try
{
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Charsert", HTTP.UTF_8);
if (reqEntity != null)
{
//添加参数
httpPost.setEntity(reqEntity);
}
HttpResponse response = client.execute(httpPost);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
HttpEntity entity = response.getEntity();
String respnseString = EntityUtils.toString(entity);
result = new JSONObject(respnseString);
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
//关闭连接
client.getConnectionManager().shutdown();
}
catch (Exception e2)
{
e2.printStackTrace();
}
}
return result;
}
}
WebServiceHelper.java
package sedion.wq.itp.webservice;
public class WebServiceHelper extends HttpClient
{
/**错误 **/
protected static final int ERROR = -1;
/**成功**/
protected static final int SUCCESS = 1;
/**失败**/
protected static final int FAILURE = 0;
/**连接超时**/
protected static final int TIME_OUT = -2;
/**补充的数据**/
protected static final String EXTRA = "extra";
/** 请求回应 **/
protected static final String RESULTS = "results";
/** IP地址或域名 **/
public static String IP = "http://192.168.2.6:8080";
/** 项目名 **/
protected static final String PROJECT = "/wmuitp/";
/** Webservice提供地址 **/
protected static String URL = IP + PROJECT + "webservice/";
/********************** 下面是方法名 **************************/
/** 用户登陆 **/
protected static final String LOGIN = "login";
}
WebService.java(用于处理方法)
package sedion.wq.itp.webservice;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import android.util.Log;
public class WebService extends WebServiceHelper
{
/**
* 用户登陆,登陆成功,返回用户的编号,用户名或密码错误,返回0,系统错误,返回-1,用户注册未审核,返回-3
* @param userName
* @param password
* @return
*/
public static int login(String userName,String password)
{
try
{
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", userName));
params.add(new BasicNameValuePair("password", password));
JSONObject result = post(URL, LOGIN, params);
System.out.println("result:"+result);
if (result!=null)
{
return result.optInt(RESULTS);
}
else
{
return ERROR;
}
}
catch (Exception e)
{
Log.e("login failure", e.toString());
return ERROR;
}
}
}
③辅助类包
StringUtil.java
package sedion.jeffli.util;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.util.Log;
/**
* 字符串操作工具类
* @author WWF
* @version 1.0
*/
public class StringUtil
{
/**
* 将字符串切割并剔除掉空的字符串
* @param value 需要切割的字符串
* @param regularExpression 分隔符
* @return 字符串数组
*/
public static String[] splitAndTrim(String value,String regularExpression)
{
try
{
String[] values = value.split(regularExpression);
int num=0;
for (int i = 0; i < values.length; i++)
{
if (!values[i].trim().equals(""))
{
num++;
}
}
String[] resultValues = new String[num];
for (int i = 0; i < resultValues.length; i++)
{
resultValues[i] = values[i];
}
return resultValues;
}
catch (Exception e)
{
Log.e("字符串切割成数组错误", e.toString());
return null;
}
}
/**
* 将List集合转化为数组
* @param list 字符串集合
* @return 字符串数组
*/
public static String[] List2Array(List<String> list)
{
try
{
String[] strings = new String[list.size()];
for (int i = 0; i < list.size(); i++)
{
strings[i] = list.get(i);
; }
return strings;
}
catch (Exception e)
{
Log.e("list转数组错误", e.toString());
return null;
}
}
/**
* 验证手机号码、电话号码是否有效
* 手机号前面加86的情况也考虑
* 新联通
*(中国联通+中国网通)手机号码开头数字 130、131、132、145、155、156、185、186
* 新移动
* (中国移动+中国铁通)手机号码开头数字 134、135、136、137、138、139、147、150、151、152、157、158、159、182、183、187、188
* 新电信
* (中国电信 +中国卫通)手机号码开头数字 133、153、189、180
* 座机:
*3/4位区号(数字)+ “-” + 7/8位(数字)+ “-”+数字位数不限
*说明:“-”+数字位数不限;这段可有可无
*/
public static String checkPhone(String phone)
{
if (null != phone)
{
String reisphoto = phone.replace(",", ",").replace(";", ",")
.replace(";", ",").replace(" ", ",").replace(" ", ",")
.replace("/", ",").replace("\\", ",");
String[] photo1 = reisphoto.split(",");
String[] photo2 = new String[photo1.length];
boolean isfirst;
if (null != photo1 && photo1.length > 0)
{
for (int i = 0; i < photo1.length; i++)
{
isfirst = false;
if (photo1[i]
.matches("(^[0-9]{3,4}-[0-9]{3,8}$)|^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|2|3|5|6|7|8|9])\\d{8}$"))
{
photo2[i] = photo1[i];
isfirst = true;
}
// 第二规则 “-”+数字位数不限 和手机号前面加86的情况也考虑
if (!isfirst)
{
if (photo1[i]
.matches("(^[0-9]{3,4}-[0-9]{3,8}-[0-9]{0,100}$)|^((\\+86)|(86))?(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|2|3|5|6|7|8|9])\\d{8}$"))
{
photo2[i] = photo1[i];
}
}
}
// 如果两个电话 只用一个
if (photo2.length > 0)
{
return photo2[0];
}
}
}
return null;
}
/**
* 判断给定字符串是否空白串。
* 空白串是指由空格、制表符、回车符、换行符组成的字符串
* 若输入字符串为null或空字符串,返回true
* @param input
* @return boolean
*/
public static boolean isEmpty(String input)
{
if (input == null || input.trim().equals(""))
{
return true;
}
for (int i = 0; i < input.length(); i++)
{
char c = input.charAt(i);
if (c != ' ' && c != '\t' && c != '\r' && c != '\n')
{
return false;
}
}
return true;
}
/**
* 验证是不是手机号码
* @param mobiles
* @return
*/
public static boolean isMobileNum(String mobiles)
{
Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
Matcher m = p.matcher(mobiles);
return m.matches();
}
}
SystemUtil.java
package sedion.jeffli.util;
import android.annotation.SuppressLint;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
/**
* 检查系统参数工具
* @author WQ
* @version 1.0
*/
public class SystemUtil
{
/**
* 检查系统的网络或wifi是否连接
* @param context 上下文
* @return 是否连通
*/
public static boolean isNetworkConnected(Context context)
{
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo network = cm.getActiveNetworkInfo();
if (network != null&&network.isConnectedOrConnecting())
{
return network.isAvailable();
}
return false;
}
/**
* 获取当前网络类型
* @return 0:没有网络 1:WIFI网络 2:WAP网络 3:NET网络
*/
@SuppressLint("DefaultLocale")
public static int getNetworkType(Context context)
{
int netType = 0;
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo == null)
{
return netType;
}
int nType = networkInfo.getType();
if (nType == ConnectivityManager.TYPE_MOBILE)
{
String extraInfo = networkInfo.getExtraInfo();
if(!StringUtil.isEmpty(extraInfo))
{
if (extraInfo.toLowerCase().equals("cmnet"))
{
netType = 3;
}
else
{
netType = 2;
}
}
}
else if (nType == ConnectivityManager.TYPE_WIFI)
{
netType = 1;
}
return netType;
}
}
UIHelper.java
package sedion.jeffli.util;
import android.app.Activity;
import android.content.Context;
import android.widget.Toast;
public class UIHelper {
/**
* 弹出Toast消息
* @param msg
*/
public static void ToastMessage(Context cont,String msg)
{
Toast.makeText(cont, msg, Toast.LENGTH_SHORT).show();
}
public static void ToastMessage(Context cont,int msg)
{
Toast.makeText(cont, msg, Toast.LENGTH_SHORT).show();
}
public static void ToastMessage(Context cont,String msg,int time)
{
Toast.makeText(cont, msg, time).show();
}
/**
* 返回原activity
* @param context
*/
public static void FinishActivity(Activity context)
{
context.finish();
}
}
终于写完代码了,下面我们试试咯!
实战说事
1.启动服务端
2.启动手机端
3.尝试登陆
你会看到
服务端:
手机端:
就ok了,如果你看到这里,谢谢你。点个赞哦!!
总结
实战真心学到很多!!
http
json
spring @ResponseBody