1 回答
TA贡献1865条经验 获得超7个赞
PHP代码没有问题,至少它可以返回一个正确的json,如果你想要的是:
{"result":"Success!","name":"YOUR_LOGIN_NAME"}
或者
{"result":"Login failed"}
在 Android 中,您应该使用 json 解析器库来解析“结果”,例如 Gson(https://github.com/google/gson)。
您可以创建一个响应类,例如:
class MyResponse {
String result;
String name;
}
然后你可以简单地调用你的代码:
@Override
protected void onPostExecute(String result) {
Gson gson = new Gson();
MyResponse response = gson.fromJson(result, MyResponse.class);
switch (response.result) {
case "Success!":
// do something when login success
// can call response.name here to read the login name
break;
case "Login failed":
// do something when login failed
break;
// and more...
}
如果您对现代 HTTP 客户端感兴趣,可以看看:
okhttp ( https://github.com/square/okhttp )
改造 ( https://github.com/square/retrofit )
- 1 回答
- 0 关注
- 129 浏览
添加回答
举报
