1 回答

TA貢獻1865條經驗 獲得超7個贊
PHP代碼沒有問題,至少它可以返回一個正確的json,如果你想要的是:
{"result":"Success!","name":"YOUR_LOGIN_NAME"}
或者
{"result":"Login failed"}
在 Android 中,您應該使用 json 解析器庫來解析“結果”,例如 Gson(https://github.com/google/gson)。
您可以創(chuàng)建一個響應類,例如:
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...
}
如果您對現(xiàn)代 HTTP 客戶端感興趣,可以看看:
okhttp ( https://github.com/square/okhttp )
改造 ( https://github.com/square/retrofit )
- 1 回答
- 0 關注
- 100 瀏覽
添加回答
舉報