【问题标题】:Integration of android app with backendandroid 应用程序与后端的集成
【发布时间】:2014-07-14 07:43:39
【问题描述】:

我正在开发一个必须与后端集成的 Android 应用程序(用 java 和 spring 开发)。哪个是集成 WebServices 或通过 http(JSON).. 的最佳方式? 提前致谢。

【问题讨论】:

    标签: android json web-services


    【解决方案1】:

    要在 Android/Java 中获取 JSON 响应,您需要这样做:

    1. 创建自定义 API 连接器类
    2. 声明将返回 JSON 数组的方法
    3. 创建 AsyncTask 类(可选)
    4. 解码 JSONArray

      1.
      public class CustomAPIConnector {
      public final String URL = "http://10.0.2.2/your-project-url/"; // 10.0.2.2 goes to computer localhost if you put localhost, it will go to the devices localhost which should not exist
      2.
      public JSONArray getUserInfo(String username, String password) {    
      
      HttpEntity httpEntity = null;
      // Add your POST variables to receive on your backend
      ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
      nameValuePairs.add(new BasicNameValuePair("username",username));
      nameValuePairs.add(new BasicNameValuePair("password",password));
      try {
          DefaultHttpClient httpClient = new DefaultHttpClient();
      
          HttpPost httpPost = new HttpPost(URL + "login.php"); // have split up URL and page so you can redirect to different links easier if the URL changes
          httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
      
      
          HttpResponse httpResponse = httpClient.execute(httpPost);
          httpEntity = httpResponse.getEntity();
      
      } catch (IOException e) {
          e.printStackTrace();
      }
      
      JSONArray jsonArray = null;
      if(httpEntity != null) {
          try {
              String entityResponse = EntityUtils.toString(httpEntity);
              jsonArray = new JSONArray(entityResponse);
          } catch (JSONException e) {
              e.printStackTrace();
          } catch(IOException e) {
              e.printStackTrace();
          }
      }
      return jsonArray;
      }
      }
      
      3.
      private class AvailableUser extends AsyncTask<ApiConnector,Boolean,JSONArray> {
      
      @Override
      protected JSONArray doInBackground(ApiConnector... params) {
          return params[0].availableUsername(etusername.getText().toString());
      }
      
      @Override
      protected void onPostExecute(JSONArray jsonArray) {
          checkAvailableUsername(jsonArray);
      }
      }
      
      private class AvailableEmail extends AsyncTask<ApiConnector,Boolean,JSONArray> {
      
      @Override
      protected JSONArray doInBackground(ApiConnector... params) {
          return params[0].availableEmail(etemail.getText().toString());
      }
      
      @Override
      protected void onPostExecute(JSONArray jsonArray) {
          checkAvailableEmail(jsonArray);
      }
      }
      4.
      private void checkAvailableEmail(JSONArray jsonArray) {
      String s = "";
      if(jsonArray != null) {
          for (int i = 0; i < jsonArray.length(); i++) {
                  JSONObject json = null;
              try {
                  json = new JSONObject();
                  json = jsonArray.getJSONObject(i);
                  if(!json.getString("count").isEmpty()) {
                      if(json.getString("count").equalsIgnoreCase("0")) {
                          status.setText("");
                          passedemail = true;
                          return;
                      } else {
                          status.setText("Email Taken");
                          passedemail = false;
                          return;
                      }
                  }
              } catch (JSONException e) {
                  e.printStackTrace();
              }
          } 
      } else {
          status.setText("Failed - checkAvailableEmail");
      }
      }
      

    注意,这是我在我的一个应用程序中注册用户的实际代码,getUserInfo 从用户那里获取所有信息,并且可用电子邮件 asynctask 类与 getUserInfo 是分开的,它是注册部分,检查电子邮件是否可用。

    从这里开始,您可以复制代码并更改您需要的内容。

    【讨论】:

    • 感谢您的代码我已经实现了...您的代码在集成时帮助了我... :)
    【解决方案2】:

    JSON 顾名思义,Java 脚本对象表示法将帮助您在后端利用 OOPS、POST/GET 和 js。 我使用 JSON,它易于编码、解析和处理

    【讨论】:

      猜你喜欢
      • 2013-12-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-13
      • 2013-08-18
      • 1970-01-01
      • 2016-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多