Updated 22 December 2016
In android, When we want to get data from web server then we have to send some request to the server by web services for transferring files such as XML and JSONÂ and HttpTransportSE for a hit to the server.
If you are making any e-commerce app or anything where you have to get the response from different-different API functions then you have to make many asyncTask for calling to the server and getting the response to the server.
For example – We are making an app where we have 3 activity(activity1, activity2, activity3)activity1 is for getting home data by getHome() apiFunctionName, activity2 is for getting some category data by getCategory() and activity3 for showing productData by showProduct() apiFunctionName.
activity1.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
import org.json.JSONObject; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.PropertyInfo; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; public class activity1 extends Activity{ protected void onCreate(Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.activity_home); //your xml file // call home connection class HomeConnection Object = new HomeConnection(); Object.execute(); } class HomeConnection extends AsyncTask < String, String, Object > { private Object mainResponse; @Override protected String doInBackground(String...arguments) { try { HttpTransportSE ht = getHttpTransportSE(); //request for Home data SoapObject mainRequest = new SoapObject(NAMESPACE, "getHome");// api function name SoapSerializationEnvelope requestEnvelop = getSoapSerializationEnvelope(mainRequest); JSONObject jo = new JSONObject(); jo.put("some", "data"); // some data send to server String jsonAsString = jo.toString(); PropertyInfo stringArrayPropertyInfo = new PropertyInfo(); stringArrayPropertyInfo.setName("attributes"); stringArrayPropertyInfo.setValue(jsonAsString); stringArrayPropertyInfo.setType(jsonAsString.getClass()); mainRequest.addProperty(stringArrayPropertyInfo); // hit to the server by ht.call method ht.call(NAMESPACE, requestEnvelop); //this is your response mainResponse = (Object) requestEnvelop.getResponse(); String mainResponseAsString = mainResponse.toString(); return mainResponseAsString; } catch (Exception e) { Log.d("Exception ", e.toString()); e.printStackTrace(); return "no"; } } protected void onPostExecute(Object backresult) { super.onPostExecute(backresult); try { // backresult, your response data is there. } catch (Exception e) { Log.d("Error onPostExecute", e.toString()); e.printStackTrace(); } } } public final SoapSerializationEnvelope getSoapSerializationEnvelope(SoapObject request) { SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.dotNet = false; envelope.xsd = SoapSerializationEnvelope.XSD; envelope.enc = SoapSerializationEnvelope.ENC; envelope.setOutputSoapObject(request); return envelope; } public final HttpTransportSE getHttpTransportSE() { HttpTransportSE ht = new HttpTransportSE(yourURL,60000); ht.debug = true; ht.setXmlVersionTag("<!--?xml version=\"1.0\" encoding= \"UTF-8\" ?-->"); return ht; } } |
activity2.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
import org.json.JSONObject; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.PropertyInfo; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; public class activity2 extends Activity{ protected void onCreate(Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.activity_category); //your xml file // call Category connection class CategoryConnection Object = new CategoryConnection(); Object.execute(); } class CategoryConnection extends AsyncTask < String, String, Object > { private Object mainResponse; @Override protected String doInBackground(String...arguments) { try { HttpTransportSE ht = getHttpTransportSE(); //request for Category data SoapObject mainRequest = new SoapObject(NAMESPACE, "getCategory");// api function name SoapSerializationEnvelope requestEnvelop = getSoapSerializationEnvelope(mainRequest); JSONObject jo = new JSONObject(); jo.put("some", "data"); // some data send to server String jsonAsString = jo.toString(); PropertyInfo stringArrayPropertyInfo = new PropertyInfo(); stringArrayPropertyInfo.setName("attributes"); stringArrayPropertyInfo.setValue(jsonAsString); stringArrayPropertyInfo.setType(jsonAsString.getClass()); mainRequest.addProperty(stringArrayPropertyInfo); // hit to the server by ht.call method ht.call(NAMESPACE, requestEnvelop); //this is your response mainResponse = (Object) requestEnvelop.getResponse(); String mainResponseAsString = mainResponse.toString(); return mainResponseAsString; } catch (Exception e) { Log.d("Exception", e.toString()); e.printStackTrace(); return "no"; } } protected void onPostExecute(Object backresult) { super.onPostExecute(backresult); try { // backresult, your response data is there. } catch (Exception e) { Log.d("Error onPostExecute", e.toString()); e.printStackTrace(); } } } public final SoapSerializationEnvelope getSoapSerializationEnvelope(SoapObject request) { SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.dotNet = false; envelope.xsd = SoapSerializationEnvelope.XSD; envelope.enc = SoapSerializationEnvelope.ENC; envelope.setOutputSoapObject(request); return envelope; } public final HttpTransportSE getHttpTransportSE() { HttpTransportSE ht = new HttpTransportSE(yourURL,60000); ht.debug = true; ht.setXmlVersionTag("<!--?xml version=\"1.0\" encoding= \"UTF-8\" ?-->"); return ht; } } |
activity3.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
import org.json.JSONObject; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.PropertyInfo; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; public class activity3 extends Activity{ protected void onCreate(Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.activity_product); //your xml file // call Product connection class ProductConnection Object = new ProductConnection(); Object.execute(); } class ProductConnection extends AsyncTask < String, String, Object > { private Object mainResponse; @Override protected String doInBackground(String...arguments) { try { HttpTransportSE ht = getHttpTransportSE(); //request for Product data SoapObject mainRequest = new SoapObject(NAMESPACE, "showProduct");// api function name SoapSerializationEnvelope requestEnvelop = getSoapSerializationEnvelope(mainRequest); JSONObject jo = new JSONObject(); jo.put("some", "data"); // some data send to server String jsonAsString = jo.toString(); PropertyInfo stringArrayPropertyInfo = new PropertyInfo(); stringArrayPropertyInfo.setName("attributes"); stringArrayPropertyInfo.setValue(jsonAsString); stringArrayPropertyInfo.setType(jsonAsString.getClass()); mainRequest.addProperty(stringArrayPropertyInfo); // hit to the server by ht.call method ht.call(NAMESPACE, requestEnvelop); //this is your response mainResponse = (Object) requestEnvelop.getResponse(); String mainResponseAsString = mainResponse.toString(); return mainResponseAsString; } catch (Exception e) { Log.d("Exception", e.toString()); e.printStackTrace(); return "no"; } } protected void onPostExecute(Object backresult) { super.onPostExecute(backresult); try { // backresult, your response data is there. } catch (Exception e) { Log.d("Error onPostExecute", e.toString()); e.printStackTrace(); } } } public final SoapSerializationEnvelope getSoapSerializationEnvelope(SoapObject request) { SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.dotNet = false; envelope.xsd = SoapSerializationEnvelope.XSD; envelope.enc = SoapSerializationEnvelope.ENC; envelope.setOutputSoapObject(request); return envelope; } public final HttpTransportSE getHttpTransportSE() { HttpTransportSE ht = new HttpTransportSE(yourURL,60000); ht.debug = true; ht.setXmlVersionTag("<!--?xml version=\"1.0\" encoding= \"UTF-8\" ?-->"); return ht; } } |
Above code will be working well. We can see that for getting 3 api function data from server we have to write  20- 30 line of code in each class for hitting the server.
But if I want to get more and more tasks to retrieve (in different ways) data from the internet and I will not be liking to increase the different classes for each single call.
So avoiding to create multiple classes for each single call, we use Reflection Technique.
I have made a method to prevent increasing of different classes to call the single hit. I have made single async task class and passed functionName and context of the activity and returned response by onPostExecute.
The async task class is there-
AsyncTaskConnection.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import org.json.JSONObject; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.PropertyInfo; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import webkul.opencart.mobikul.SplashScreen; import webkul.opencart.mobikul.credentials.AppCredentials; import android.app.ProgressDialog; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.AsyncTask; import android.util.Log; public class AsyncTaskConnection extends AsyncTask<String, String, Object>{ ProgressDialog progress; Object response = null, mainResponse = null; JSONObject mainObject; Context mContext; SharedPreferences configShared; public Object customerLoginResponse; public Editor editor; String returnFunctionName; private String pageNumber; public AsyncTaskConnection (Context context){ mContext = context; } protected void onPreExecute() { super.onPreExecute(); } @Override protected Object doInBackground(String... arguments) { String apiFunctionName = arguments[0]; // get api FunctionName String jsonString = arguments[1]; // get data which want to send to the server returnFunctionName = apiFunctionName+"Response"; // response function name try{ HttpTransportSE ht = getHttpTransportSE(); // request for customer login SoapObject Request = new SoapObject(NAMESPACE, apiFunctionName); SoapSerializationEnvelope requestEnvelop = getSoapSerializationEnvelope(Request); JSONObject jo = new JSONObject(jsonString); String jsonAsString = jo.toString(); PropertyInfo stringArrayPropertyInfo = new PropertyInfo(); stringArrayPropertyInfo.setName("attributes"); stringArrayPropertyInfo.setValue(jsonString); stringArrayPropertyInfo.setType(jsonString.getClass()); Request.addProperty(stringArrayPropertyInfo); ht.call(NAMESPACE, requestEnvelop); Object Response = (Object) requestEnvelop.getResponse(); String ResponseAsString = Response.toString(); return ResponseAsString; } catch (Exception e) { Log.d("Exception", e.toString()); return "no"; } } // main thing is there, we have used the reflaction here @Override protected void onPostExecute(Object backresult) { Method m; try { m = mContext.getClass().getDeclaredMethod(returnFunctionName, String.class); m.invoke(mContext, (String) backresult); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } private final SoapSerializationEnvelope getSoapSerializationEnvelope(SoapObject request) { SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.dotNet = false; envelope.xsd = SoapSerializationEnvelope.XSD; envelope.enc = SoapSerializationEnvelope.ENC; envelope.setOutputSoapObject(request); return envelope; } private final HttpTransportSE getHttpTransportSE() { HttpTransportSE ht = new HttpTransportSE(AppCredentials.URL, 60000); ht.debug = true; ht.setXmlVersionTag("<!--?xml version=\"1.0\" encoding= \"UTF-8\" ?-->"); return ht; } } |
and in caller class(activity1,activity2,activity3)
activity1.java
replace
1 2 3 |
// connection call HomeConnection Object = new HomeConnection(); Object.execute(); |
to
1 2 3 4 |
//call this class where you want and get dynamic response JSONObject jo = new JSONObject(); jo.put("some", "data"); new AsyncTaskConnection(this).execute("getHome",jo.toString()); |
and make response function to your class
1 2 3 4 5 6 7 8 9 10 |
// and make response function public void getHomeResponse(String backresult) { try { // this is your response mainObject = new JSONObject(backresult); } catch (JSONException e) { e.printStackTrace(); } } |
Like this-
activity1.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import org.json.JSONObject; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; public class activity1 extends Activity{ protected void onCreate(Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.activity_home); // this methosd for call connection JSONObject jo = new JSONObject(); jo.put("some", "data");// create data to send to the server new AsyncTaskConnection(this).execute("getHome",jo.toString()); } // and make response function public void getHomeResponse(String backresult) { try { mainObject = new JSONObject(backresult); } catch (JSONException e) { e.printStackTrace(); } } } |
activity2.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import org.json.JSONObject; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; public class activity2 extends BaseActivity{ protected void onCreate(Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.activity_category); // this methosd for call connection JSONObject jo = new JSONObject(); jo.put("some", "data");// create data to send to the server new AsyncTaskConnection(this).execute("getCategory",jo.toString()); } // and make response function public void getCategoryResponse(String backresult) { try { mainObject = new JSONObject(backresult); } catch (JSONException e) { e.printStackTrace(); } } } |
activity3.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import org.json.JSONObject; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; public class activity3 extends Activity{ protected void onCreate(Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.activity_product); // this methosd for call connection JSONObject jo = new JSONObject(); jo.put("some", "data");// create data to send to the server new AsyncTaskConnection(this).execute("showProduct",jo.toString()); } // and make response function public void showProductResponse(String backresult) { try { mainObject = new JSONObject(backresult); } catch (JSONException e) { e.printStackTrace(); } } } |
By using reflection method, we can avoid writing different classes for each single call.
Simply we have to pass context of the activity, API function name and data to send in JSON form.
And get the response where your want.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.