与后端Android应用整合 [英] Integration of android app with backend

查看:133
本文介绍了与后端Android应用整合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Android应用程序,我必须与后端系统集成(Java和春季开发)。这将是整合Web服务两种或通过HTTP(JSON)的最佳方法..?
提前致谢。

i am developing an android app which i have to integrate with backend(developed in java and spring). Which will be the best way to integrate either WebServices or through http(JSON)..? Thanks in advance.

推荐答案

要获得的Andr​​oid / Java中的JSON响应,你需要做的是:

To get a JSON Response in Android/Java you need to do this:


  1. 创建一个自定义的API接口类

  2. 声明,将返回一个JSON阵列的方法

  3. 创建一个AsyncTask的类(可选)

  4. 德code JSONArray

  1. Create a custom API Connector class
  2. Declare a method that will return a JSON Array
  3. Create a AsyncTask class (optional)
  4. Decode 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");
}
}


注意,这是实际code我在我的应用程序,registersa用户之一,getUserInfo从用户获取所有信息,可用电子邮件的AsyncTask类是独立的getUserInfo,它是注册的一部分,来检查,如果该电子邮件是可用的。

Please note that this is actual code I have in one of my apps that registersa user, the getUserInfo gets all information from the user, and the Available email asynctask class is separate from the getUserInfo, it is the registering part, that checks if the email is available.

从这里开始,您可以复制code和改变你需要什么。

From here on, you can copy the code and change what you need to.

这篇关于与后端Android应用整合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆