Thursday, 5 November 2015

Android API V - 23 NameValuePair Deprecated Solution

Hi 

By Brand New Android API V-23 update, all of our android developers are facing a big issue on "Apache HTTP Client Removal". In my circle also by update of API V-23 lots of developer unable to connect with sever Page.

Apache Removed Reference :

org.apache.http.NameValuePair;
org.apache.http.message.BasicNameValuePair;
Android 6.0 release removes support for the Apache HTTP client. But in our existing Android Projects we connected & Receive the JSON through the server page by using the Apache's NameValuePair & BasicNameValuePair till API V-22.

 Because of deprecated of Apache, we need to use HttpURLConnection, But the problem is have to change & correct all of your android Logic & Code in all projects.

My self i have try to find the solution by using the HttpURLConnection without changing your Logic & Code. i took small risk for you,  just by relax you may download my .AAR file and attach into your project and run with your same code by small changes.

(If it is useful to you don't forget to leave a comment)

Download my raj_namevaluepair.aar file & add it into your project.


Android Code to Use My .AAR file :


// my server page link
String URL_My_Server_Page="http://xxxxx.com/xxxxx/xxxxxxxx/xxxxxxx.php";
@Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_jsonparser);
    tv_op = (TextView)findViewById(R.id.tv_op);
    listView = (ListView) findViewById(R.id.listView);
// class to execute the URL
new Page_Executer().execute();
}

class Page_Executer extends AsyncTask<String, String, String> {
    ProgressDialog p_dialog;
    String op_esult = null;
    @Override    protected void onPreExecute() {
        super.onPreExecute();
        p_dialog = new ProgressDialog(JSONParser.this);
        p_dialog.setTitle("Loading");
        p_dialog.setCancelable(false);
        p_dialog.setIndeterminate(false);
        p_dialog.show();
    }

    @Override    protected String doInBackground(String... strings) {
        a_list = new ArrayList();
        try {
// My class to handle            
R_JSONParser raj_parser = new R_JSONParser();
// My NameValuePair to pass your parameters            
ArrayList<R_NameValuePair> n_pair = new ArrayList<>();
// can pass number of parameters as you need            
n_pair.add(new R_NameValuePair("",""));
// method to return the result as string , you need to pass server page link, Method, Parameters with values            
op_esult = raj_parser.Load_Data_From_URL(URL_My_Server_Page, "GET", n_pair);
            Log.d("result", op_esult);
// As usual you can handle JSONArray & JSONObject            
JSONArray j_array = new JSONArray(op_esult);
            for (int i = 0; i < j_array.length(); i++) {
                JSONObject j_obj_t = j_array.getJSONObject(i);
                String MS01key = j_obj_t.getString("MS01key");
                String BloodGroupName = j_obj_t.getString("BloodGroupName");
                a_list.add(new Raj_B(MS01key, BloodGroupName));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        Raj_Adapter r_adptr = new Raj_Adapter(getApplicationContext(), R.layout.list_a, a_list);
        listView.setAdapter(r_adptr);
        p_dialog.dismiss();
    }
}


The Output is :



Enjoy..,

Have A Happy Day..,