【问题标题】:Android get Returned value from phpAndroid从php获取返回值
【发布时间】:2014-07-31 02:58:46
【问题描述】:

我正在将我的 Android 应用程序连接到我的 Drupal 网站。在我的应用程序中,我正在使用标准 PHP user_save 为该网站创建用户帐户。我需要将我的应用程序从当前活动移动到下一个活动是从创建 Drupal 用户帐户的 PHP 表单返回的值。

所以当一个帐户被创建时,'invalid' 或 'valid' 的值将从 php 表单返回,如下所示:

  // 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 方法中检索有效或无效的返回值,以便我可以使用它来做出其他决定,例如尝试 agin 或更改活动?

我已经更新了上面的代码

这是我的应用程序中发送用户数据的部分:

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
}

【问题讨论】:

    标签: java php android drupal-7 return


    【解决方案1】:

    最简单的做法是从您的 PHP 中返回 JSON 或 XML,然后您的应用程序可以读取和解析它们。

    实际上,您甚至可以在页面上打印“有效”或“无效”,然后在您的应用中使用JSoup 之类的内容来读取网页并提取字符串。

    【讨论】:

      猜你喜欢
      • 2013-02-14
      • 2021-11-20
      • 1970-01-01
      • 2017-09-04
      • 1970-01-01
      • 2021-12-24
      • 2015-10-06
      • 1970-01-01
      • 2014-05-04
      相关资源
      最近更新 更多