嘿伙计我是Android网络概念的新手。我想从android app发送用户名,密码,imei号码和位置到php服务器。我完成了我的发送部分。现在我的问题是如何接收响应。我希望得到状态(1或0)根据我想要移动到下一页。所以任何人都会知道如何做到这一点,欢迎你。
private static final String REGISTER_URL="http://vPC70.com/App/login.php";
username = editTextUserName.getText().toString().toLowerCase();
userpassword=editTextPassword.getText().toString().toLowerCase();
loc="11.295756,77.001890";
imeino = "12312312456";
register(username, userpassword, imeino, loc);
private void register(final String username, final String userpassword,
String imeino, String loc) {
String urlSuffix = "?
username="+username+"&userpassword="+userpassword+"&imeino="+imeino
+"&location="+loc;
class RegisterUser extends AsyncTask<String,String , String>{
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(LoginActivity.this, "Please
Wait",null, true, true);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
}
@Override
protected String doInBackground(String... params) {
String s = params[0];
BufferedReader bufferedReader = null;
try {
URL url = new URL(REGISTER_URL+s);
HttpURLConnection con = (HttpURLConnection)
url.openConnection();
bufferedReader = new BufferedReader(new
InputStreamReader(con.getInputStream()));
String result;
result = bufferedReader.readLine();
return result;
}catch(Exception e){
return null;
}
}
}
RegisterUser ru = new RegisterUser();
ru.execute(urlSuffix);
这是回复
{"Login":[{"status":"1","message":"Login Successfully !!!"}]}
{"Login":[{"status":"0","message":"Invalid Password !!!"}]}
如果响应是1 toast,则消息登录成功 如果响应为0,则在执行后执行消息无效密码
答案 0 :(得分:0)
在 onPostExecute(String s)中,您可以将结果转换为json并检查状态值,如
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
JsonObject object = new JsonObject(s);
if(object.optString("status").equals("1"))
{
// Your Logic here
}
}
答案 1 :(得分:0)
创建POJO / Model类以转换您的Response。
喜欢这个
public class LoginResponse{
@SerializedName("Login")
@Expose
private List<Login> login = null;
public List<Login> getLogin() {
return login;
}
public void setLogin(List<Login> login) {
this.login = login;
}
}
public class Login {
@SerializedName("status")
@Expose
private String status;
@SerializedName("message")
@Expose
private String message;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
PostExcecute中的使用GSON
将响应转换为POJO对象 Gson gson = new Gson();
LoginResponse response = gson.toJson(result, LoginResponse.class);
您可以在这里查看条件:;
if(response !=null && response.getLogin() !=null)
{
if(response.getLogin().getStatus().equalIgnoreCase("1"))
{
// show toast Login Successfully !!! and move to next screen
}
else if(response.getLogin().getStatus().equalIgnoreCase("0"))
{
// Invalid Password !!! your logic here
}
}
答案 2 :(得分:0)
这是根据您的响应字符串
的解析器 private void parseResponseJson(String response) throws JSONException {
JSONObject jsonObject = new JSONObject(response).getJSONArray("Login").getJSONObject(0);
String status = jsonObject.getString("status");
String message = jsonObject.getString("message");
}
答案 3 :(得分:0)
从服务器获取响应后,根据状态在toast中显示消息
try {
JSONObject jobj = new JSONObject(response);
String status = jobj.getString("status");
String msg = jobj.getString("message");
if (status.equals("1")) {
//move to next page
Toast.makeText(LoginActivity.this, msg,Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(LoginActivity.this, msg,Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
答案 4 :(得分:0)
简单高效的解决方案。使用Google的Gson库。您可以像这样从json字符串轻松创建哈希图。
Type type = new TypeToken<Map<String, String>>(){}.getType();
Map<String, String> myMap = gson.fromJson(JSONString, type);