Menus

Showing posts with label Android mysql get method. Show all posts
Showing posts with label Android mysql get method. Show all posts

Wednesday 8 May 2019

Android AsyncTask HTTP GET request Tutorial


More details about Android Asynchronous Http Client

Here is simple HttpsURLConnection in ASyncTask class for Https GET web-API calling along with packet-header and JSONObject in body.


Get JSON value show in RecyclerView.

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_courses);

        recyclerCourse = (RecyclerView) findViewById(R.id.recyclerCourse);
        recyclerCourse.setHasFixedSize(true);
        recyclerCourse.setLayoutManager(new LinearLayoutManager(this));

        listCourses = new ArrayList<>();

        AsyncHttpClient client = new AsyncHttpClient();
        RequestParams params = new RequestParams();

        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Please Wait....");
        progressDialog.show();

        client.get(Config.URL_COURSEDIPLAY, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

                try {
                    JSONArray arr = new JSONArray(new String(responseBody));

                    for(int i=0; i<arr.length();i++){
                        JSONObject obj = (JSONObject)arr.get(i);


                        ListCourse listCourse = new ListCourse(
                                obj.get("course").toString(),
                                obj.get("slid").toString()
                        );
                        listCourses.add(listCourse);

                    } //for int=0

                    progressDialog.dismiss();
                    Toast.makeText(getApplicationContext(), "Loading Complete", Toast.LENGTH_LONG).show();


                } catch (JSONException e){
                    progressDialog.dismiss();
                    // TODO Auto-generated catch block
                    Toast.makeText(getApplicationContext(), "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

                progressDialog.dismiss();
                // TODO Auto-generated method stub
                if(statusCode == 404){
                    Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
                }else if(statusCode == 500){
                    Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
                }else{
                    Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet]", Toast.LENGTH_LONG).show();
                }

            }
        });

        adapter = new CourseAdapter(listCourses,this);
        recyclerCourse.setAdapter(adapter);

    }


URL defined in Config class
private static final String ROOT_URL = "http://10.42.0.1/dsms/android/";
public static final String URL_COURSEDIPLAY = ROOT_URL + "displaycourse.php";


In Android Studio, use the Gradle entry as:

compile 'com.loopj.android:android-async-http:1.4.9'compile 'com.google.code.gson:gson:2.2.+'