0

I'm learning android by working on a simple project. I have the layout completed and I'm at a point where I need to communicate with the back-end. I've worked with PHP/JSON a lot before and I know exactly what I need to do on the back-end. I have the following two questions,

1 - What kind of adapter I should be using when dealing with JSON? please note that the back-end will be sending 10 records at a time, and the user will scroll up to get the next 10 so the dataset will be changing as the user scrolls the view

2 - I'll be using HTTP to get the JSON data mentioned in point 1, is there a preferred method in android to be used for communication with the back-end?

Please note that I don't want to Parse or any other cloud solutions.

user2628572
  • 945
  • 1
  • 8
  • 14

2 Answers2

1

Android has built-in JSON parsing capability and an Http client. Take a look at this stackoverflow post, which has step-by-step instructions for making the Http request and parsing the returned JSON data:

How to parse JSON in Android

However, this post uses the older DefaultHttpClient. This is only recommended for Froyo and below. For newer code, Google recommends that you use HttpURLConnection instead (on Gingerbread and higher API systems). Their function is very similar, here is the reference for Android's HttpURLConnection:

HttpURLConnection | Android Developers

Community
  • 1
  • 1
savanto
  • 4,470
  • 23
  • 40
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");

    }
Ahmad
  • 437
  • 1
  • 4
  • 12