Java 尝试使用状态代码验证来自android json post的登录
我正在尝试使用下面的代码验证登录。我的挑战是如何获得200状态码的响应,如果是,则显示欢迎屏幕。这是我的代码尝试,但它没有状态来确认post是否成功,然后采取下一个操作Java 尝试使用状态代码验证来自android json post的登录,java,php,android,json,Java,Php,Android,Json,我正在尝试使用下面的代码验证登录。我的挑战是如何获得200状态码的响应,如果是,则显示欢迎屏幕。这是我的代码尝试,但它没有状态来确认post是否成功,然后采取下一个操作 public void executeLoginValidation() { Map<String, String> comment = new HashMap<String, String>(); comment.put("email", loginActivityE
public void executeLoginValidation() {
Map<String, String> comment = new HashMap<String, String>();
comment.put("email", loginActivityEmail.getText().toString());
comment.put("password", loginActivityPassword.getText().toString());
String json = new GsonBuilder().create().toJson(comment, Map.class);
makeRequest("http://localhost:88/API/web/app_dev.php/validatelogin/", json);
}
public static HttpResponse makeRequest(String uri, String json) {
try {
HttpPost httpPost = new HttpPost(uri);
httpPost.setEntity(new StringEntity(json));
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
return new DefaultHttpClient().execute(httpPost);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public void executeLoginValidation(){
Map comment=newhashmap();
comment.put(“email”,loginActivityEmail.getText().toString());
comment.put(“password”,loginActivityPassword.getText().toString());
字符串json=new GsonBuilder().create().toJson(comment,Map.class);
makeRequest(“http://localhost:88/API/web/app_dev.php/validatelogin/“,json);
}
公共静态HttpResponse makeRequest(字符串uri、字符串json){
试一试{
HttpPost HttpPost=新的HttpPost(uri);
setEntity(新的StringEntity(json));
setHeader(“接受”、“应用程序/json”);
setHeader(“内容类型”、“应用程序/json”);
返回新的DefaultHttpClient().execute(httpPost);
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回null;
}
请告诉我如何修改上述表单post代码以返回状态代码,然后在登录屏幕上执行必要的步骤,正如前面的另一位所说,请尝试从HttpResponse获取状态代码:
public void executeLoginValidation() {
Map<String, String> comment = new HashMap<String, String>();
comment.put("email", loginActivityEmail.getText().toString());
comment.put("password", loginActivityPassword.getText().toString());
String json = new GsonBuilder().create().toJson(comment, Map.class);
HttpResponse response = makeRequest("http://localhost:88/API/web/app_dev.php/validatelogin/",json);
int statusCode = response.getStatusLine().getStatusCode();
if(statusCode == 200){
showSplashScreen();
}else{
//ErrorHandling
}
}
public void executeLoginValidation(){
Map comment=newhashmap();
comment.put(“email”,loginActivityEmail.getText().toString());
comment.put(“password”,loginActivityPassword.getText().toString());
字符串json=new GsonBuilder().create().toJson(comment,Map.class);
HttpResponse response=makeRequest(“http://localhost:88/API/web/app_dev.php/validatelogin/“,json);
int statusCode=response.getStatusLine().getStatusCode();
如果(状态代码==200){
showSplashScreen();
}否则{
//错误处理
}
}
正如另一位在我前面所说的,尝试从HttpResponse获取状态代码:
public void executeLoginValidation() {
Map<String, String> comment = new HashMap<String, String>();
comment.put("email", loginActivityEmail.getText().toString());
comment.put("password", loginActivityPassword.getText().toString());
String json = new GsonBuilder().create().toJson(comment, Map.class);
HttpResponse response = makeRequest("http://localhost:88/API/web/app_dev.php/validatelogin/",json);
int statusCode = response.getStatusLine().getStatusCode();
if(statusCode == 200){
showSplashScreen();
}else{
//ErrorHandling
}
}
public void executeLoginValidation(){
Map comment=newhashmap();
comment.put(“email”,loginActivityEmail.getText().toString());
comment.put(“password”,loginActivityPassword.getText().toString());
字符串json=new GsonBuilder().create().toJson(comment,Map.class);
HttpResponse response=makeRequest(“http://localhost:88/API/web/app_dev.php/validatelogin/“,json);
int statusCode=response.getStatusLine().getStatusCode();
如果(状态代码==200){
showSplashScreen();
}否则{
//错误处理
}
}
尝试使用response.getStatusLine().getStatusCode()从响应(HttpResponse
)获取状态代码代码>尝试使用response.getStatusLine().getStatusCode()从响应(HttpResponse
)获取状态代码代码>