Android应用程序与后端的集成

时间:2014-07-14 07:43:40

标签: android json web-services

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

2 个答案:

答案 0 :(得分:0)

JSON作为名称说java脚本对象表示法将帮助您在后端利用OOPS,POST / GET和js。 我使用JSON,它易于编码,解析和处理

答案 1 :(得分:0)

要在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");
    }
    }
    
  5. 请注意,这是我在其中一个注册用户的应用程序中的实际代码,getUserInfo从用户获取所有信息,并且Available email asynctask类与getUserInfo分开,它是注册部分,用于检查电子邮件是否可用。

    从此处开始,您可以复制代码并更改所需内容。