Android - 与后端通信

时间:2014-04-26 19:12:06

标签: android

我正在通过处理一个简单的项目来学习android。我已完成布局,而且我需要与后端进行通信。我之前使用过PHP / JSON很多,而且我确切地知道我需要在后端做些什么。我有以下两个问题,

1 - 在处理JSON时我应该使用哪种适配器?请注意,后端将一次发送10条记录,用户将向上滚动以获得接下来的10条记录,以便在用户滚动视图时数据集将发生变化

2 - 我将使用HTTP来获取第1点中提到的JSON数据,android中是否有一个首选方法用于与后端进行通信?

请注意,我不想解析或任何其他云解决方案。

2 个答案:

答案 0 :(得分:1)

Android具有内置的JSON解析功能和Http客户端。看一下这个stackoverflow帖子,它有关于发出Http请求和解析返回的JSON数据的分步说明:

How to parse JSON in Android

但是,这篇文章使用旧的DefaultHttpClient。这仅适用于Froyo及以下。对于较新的代码,Google建议您使用HttpURLConnection(在Gingerbread和更高版本的API系统上)。它们的功能非常相似,这里是Android的HttpURLConnection的参考:

HttpURLConnection | Android Developers

答案 1 :(得分:1)

1. You will have to have a something which will communicate with the server for that just copy this code :-

public class ConnectionClass 
{
    Context context;
    public ConnectionClass(Context ctx) {
        this.context=ctx;
    }
    public String connectToServer(String urlLink)
    {

        try 
        {

            HttpClient client = new DefaultHttpClient();
            HttpPost http_get = new HttpPost(urlLink);



            HttpResponse responses;
            responses = client.execute(http_get);
            if (responses != null) 
            {
                InputStream in = responses.getEntity().getContent(); 
                String a = convertStreamToString(in);
                return a;
            }

        } 
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    String convertStreamToString(InputStream is) {

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try
        {
            while ((line = reader.readLine()) != null) 
            {
                sb.append(line);
            }
        } 
        catch (Exception e)
        {

             //Toast.makeText(context, e.toString()+" io2", Toast.LENGTH_LONG).show();
        } 
        finally 
        {
            try 
            {
                is.close();
            } 
            catch (Exception e)
            {

            }
        }
        return sb.toString();
    }
}

 2. To use that and keeping in mind dat newer version of android does not allow executing network operations on mainthread i will give a simple example to run on onCreate() of the activity using AsyncTask  this is something like Ajax in web .





    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        context=this;
        ConnectionClass cc=new ConnectionClass(context);
        new AsyncTask<String, Void, String>() 
        {


            @Override
            protected String doInBackground(String... arg) {
                String data=cc.connectToServer(arg[0]);
                return data;
            }

            protected void onPostExecute(String result) 
            {
                super.onPostExecute(result);
                try 
                {
                    JSONObject jobj=new JSONObject(result);
                    String idval=jobj.getString("id");
                    Toast.makeToast(context,idval,2000).show();
                } catch (Exception e) 
                {
                    e.printStackTrace();
                }
            }
        }.execute("http://mydomain.com/fetchjson.php");

    }