我正在连接我的Android应用程序与我的Drupal网站。在我的应用程序中,我使用标准PHP user_save创建该网站的用户帐户。我需要将我的应用程序从当前活动移动到下一个是要从PHP表单返回的值,该PHP表单正在创建我的Drupal用户帐户。
因此,当创建一个帐户时,将从php表单返回'invalid'或'valid'值,如下所示:
// User Authentication
$responsep="valid";
$responsen="invalid";
if(!user_authenticate($username, $password)){
drupal_json_output($responsen);
}else{
// Logs the USER in
$account = user_authenticate($username, $password);
$user = user_load($account, TRUE);
drupal_session_regenerate();
drupal_json_output($responsep);
}
问题是,我如何,在我的android java方法中,检索有效或无效的返回值,以便我可以使用它来做出其他决定,如再次尝试或更改活动?
我已经更新了上面的代码
这是我的应用程序发送用户数据的部分:
public void createUserAccount(String usernameFinal, String passwordFinal,
String userEmail) throws ClientProtocolException, IOException {
// POST Username data to php
String uri = "https://url/hstusercreate.php";
Log.d("Action", "Posting user data to php");
HttpClient client = new DefaultHttpClient();
HttpPost getMethod = new HttpPost(uri);
Log.d("Posting Location", uri);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
Log.d("Posting Username", usernameFinal);
nameValuePairs.add(new BasicNameValuePair("username", usernameFinal));
Log.d("Posting Pass", passwordFinal);
nameValuePairs.add(new BasicNameValuePair("password", passwordFinal));
//Log.d("Posting Email", userEmail);
//nameValuePairs.add(new BasicNameValuePair("email", userEmail));
getMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
client.execute(getMethod);
Log.d("Action", "Finished Posting Data to PHP");
// Return the message from php
// If Successful then stop the creation of another user
}
最简单的方法是从PHP返回JSON或XML,然后应用程序可以读取和解析。
实际上你甚至可以在页面上打印"valid"或"invalid",然后在你的应用程序中使用类似JSoup的东西来读取网页并提取字符串