public class LoginProtocol extends BaseProtocol{
private List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
private final static String URL = "http://192.168.1.110:8080/healthServer/login";
public boolean checkLogin(User user){
try {
addNameValuePair("name", user.getUsername());
addNameValuePair("password", user.getPassword());
pack(URL);
System.out.println("URL是:::::::"+URL);
parse();
JSONObject obj = this.getJSON();
System.out.println("输出obj是"+obj);
if(obj.getString("result").equals("false")){
return false;
}else{
return true;
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("mmmmmmmmmmmmmmmmm");
return false;
}
}
}
public class BaseProtocol {
private StringBuilder sb = new StringBuilder();
private HttpClient httpClient;
private HttpPost httpRequest;
private HttpResponse response;
private List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
BaseProtocol() {
httpClient = new DefaultHttpClient();
}
/**
* 向服务器端发送请求
*
* @param url
* @throws Exception
*/
protected void pack(String url) throws Exception {
httpClient = new DefaultHttpClient();
httpRequest = new HttpPost(url);
try{
httpRequest.setEntity(new UrlEncodedFormEntity(nameValuePair,HTTP.UTF_8));
//取得HTTP response
System.out.println("httpRequest是"+httpRequest);
System.out.println(nameValuePair+"//////////////////////");
System.out.println("entity是;;;;;;;;;"+httpRequest.getEntity());
response = httpClient.execute(httpRequest);
System.out.println(response);
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 得到返回数据
*
* @param url
* @return
* @throws Exception
*/
protected void parse() throws Exception {
// TODO 状态处理 500 200
if (response.getStatusLine().getStatusCode() == 200) {
System.out.println("ssssssssssssssssss"+response.getStatusLine().getStatusCode());
BufferedReader bufferedReader2 = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
System.out.println("jjjjjjjjjjjjjjjjjjjjjjjjj"+bufferedReader2);
for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2
.readLine()) {
sb.append(s);
System.out.println(sb+"****************");
}
System.out.println("执行Parse()中的If语句了");
}
}
/**
* 向服务器发送信息
*
* @param key
* @param value
*/
public void addNameValuePair(String key, String value) {
nameValuePair.add(new BasicNameValuePair(key, value));
System.out.println(nameValuePair);
}
/**
* 返回JSONObject对象数据模型
*
* @return
* @throws JSONException
*/
public JSONObject getJSON() throws JSONException {
return new JSONObject(sb.toString());
}
}
struts.xml文件:
/main/hint/errorpage/error.jsp
/main/hint/errorpage/javaerror.jsp
/WEB-INF/main/message.jsp
<action name="login" class="action.login.LoginAction" method="logins">
<result name="success2" type="json"><param name="includeProperties">result</param>
<param name="ignoreHierarchy">false</param></result>
<result name="success1" >/main/loginSuccess.jsp</result>
<result name="false" >/main/loginFail.jsp</result>
</action>
在loginAnction中我应该怎么处理,怎么接收android传过来的HttpPost,怎么将httpRequest.setEntity(new UrlEncodedFormEntity(nameValuePair,HTTP.UTF_8))中的NameValuePair中的用户信息读出来
或者,我在服务端应该怎么处理,请大神们指点一下,项目现在需要写web和android同时写