What is the most efficient way on Android to call HTTP Web API calls that return a JSON response?
What is the most efficient way on Android to call HTTP Web API calls that return a JSON response?
I'm the perfectionist type, I already got web API calls working fine with Google Places API (just as an example), but I feel it's sometimes slow or maybe I'm not doing it right. Some blogs are saying I should use AndroidHttpClient, but I'm not, should I ?
The web API calls i'm using return json and I don't run them on the UI thread, hence using AsyncTask (is AsyncTask the most efficient way to run on background thread or should I use something else ?)
Please see my code and tell me how could it be more efficient in anyway
public static class NearbySearchRequest extends AsyncTask { Exception mException = null; @Override protected void onPreExecute() { super.onPreExecute(); this.mException = null; } @Override protected JSONObject doInBackground(String... params) { StringBuilder urlString = new StringBuilder(); urlString.append("https://maps.googleapis.com/maps/api/place/nearbysearch/json?"); urlString.append("key=").append(Constants.GOOGLE_SIMPLE_API_KEY); urlString.append("&location=").append(params[0]); urlString.append("&sensor=").append("true"); urlString.append("&language=").append("en-GB"); urlString.append("&name=").append(params[1]); urlString.append("&rankby=").append("distance"); LogHelper.Log(urlString.toString()); HttpURLConnection urlConnection = null; URL url = null; JSONObject object = null; try { url = new URL(urlString.toString()); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.setDoOutput(true); urlConnection.setDoInput(true); urlConnection.connect(); InputStream inStream = null; inStream = urlConnection.getInputStream(); BufferedReader bReader = new BufferedReader(new InputStreamReader(inStream)); String temp, response = ""; while ((temp = bReader.readLine()) != null) response += temp; bReader.close(); inStream.close(); urlConnection.disconnect(); object = (JSONObject) new JSONTokener(response).nextValue(); } catch (Exception e) { this.mException = e; } return (object); } @Override protected void onPostExecute(JSONObject result) { super.onPostExecute(result); if (this.mException != null) ErrorHelper.report(this.mException, "Error # NearbySearchRequest"); } }
Answer by gunar for What is the most efficient way on Android to call HTTP Web API calls that return a JSON response?
The Http engine you're using seems the best choice. Actually any other 3-rd party engines are based either on Apache, either on HttpUrlConnection. I prefer to use Spring for Android as that API provide an abstraction over Http Engine and you don't really need to care how about what API to use based on API level. Or you can use Volley - a very fashionable library.
I would touch however some of your code:
What if there is an exception while reading the stream? Then the stream remains open and also the connection. So I would suggest to have a finally block where the streams and connection is closed no matter if you get an exception or not:
HttpURLConnection urlConnection = null; URL url = null; JSONObject object = null; InputStream inStream = null; try { url = new URL(urlString.toString()); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.setDoOutput(true); urlConnection.setDoInput(true); urlConnection.connect(); inStream = urlConnection.getInputStream(); BufferedReader bReader = new BufferedReader(new InputStreamReader(inStream)); String temp, response = ""; while ((temp = bReader.readLine()) != null) { response += temp; } object = (JSONObject) new JSONTokener(response).nextValue(); } catch (Exception e) { this.mException = e; } finally { if (inStream != null) { try { // this will close the bReader as well inStream.close(); } catch (IOException ignored) { } } if (urlConnection != null) { urlConnection.disconnect(); } }
JSON parsing: you're using the Android standard way of parsing JSON, but that's not the fastest and easiest to work with. GSON and Jackson are better to use. To make a comparison when it comes for JSON parsers, I would go for Jackson. Here's another SO topic on this comparison.
Don't concatenate strings like that as concatenating strings will create each time another string. Use a
StringBuilder
instead.Exception handling (this is anyway a long-debate subject in all programming forums). First of all you have to log it (Use
Log
class notSystem.out.printXXX
). Then you need to either inform the user: either you toast a message, either you show a label or notification. The decision depends on the user case and how relevant is the call you're making.
These are the topics I see in you code.
EDIT I realize I didn't answer this: is AsyncTask the most efficient way to run on background thread or should I use something else?
The short answer I would give is: if you're supposed to perform a short time lived request, then AsyncTask
is perfect. However, if you need to get some data and display it - but you don't want to worry about whether to download again if the screen is rotated and so on, I would strongly recommend using an AsyncTaskLoader
and Loaders
in general.
If you need to download some big data, then either you use an IntentService
or, for heavy-weight operations, DownloadManager
.
Enjoy coding!
Answer by Nikunj Desai for What is the most efficient way on Android to call HTTP Web API calls that return a JSON response?
------Create a Service Handler Class to your Project-------- public class ServiceHandler {
static String response = null; public final static int GET = 1; public final static int POST = 2; public ServiceHandler() { } /* * Making service call * @url - url to make request * @method - http request method * */ public String makeServiceCall(String url, int method) { return this.makeServiceCall(url, method, null); } /* * Making service call * @url - url to make request * @method - http request method * @params - http request params * */ public String makeServiceCall(String url, int method, List params) { try { // http client DefaultHttpClient httpClient = new DefaultHttpClient(); HttpEntity httpEntity = null; HttpResponse httpResponse = null; // Checking http request method type if (method == POST) { Log.e("in POST","in POST"); HttpPost httpPost = new HttpPost(url); // adding post params if (params != null) { Log.e("in POST params","in POST params"); httpPost.setEntity(new UrlEncodedFormEntity(params)); } Log.e("url in post service",url); httpResponse = httpClient.execute(httpPost); } else if (method == GET) { // appending params to url Log.e("in GET","in GET"); if (params != null) { Log.e("in GET params","in GET params"); String paramString = URLEncodedUtils .format(params, "utf-8"); url += "?" + paramString; } Log.e("url in get service",url); HttpGet httpGet = new HttpGet(url); httpResponse = httpClient.execute(httpGet); } httpEntity = httpResponse.getEntity(); response = EntityUtils.toString(httpEntity); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return response; } public String makeServiceCallIMAGE(String url, int method, List params) { try { // http client DefaultHttpClient httpClient = new DefaultHttpClient(); HttpEntity httpEntity = null; HttpResponse httpResponse = null; // Checking http request method type if (method == POST) { HttpPost httpPost = new HttpPost(url); // adding post params if (params != null) { httpPost.setEntity(new UrlEncodedFormEntity(params)); } httpResponse = httpClient.execute(httpPost); } else if (method == GET) { // appending params to url if (params != null) { String paramString = URLEncodedUtils .format(params, "utf-8"); url += "?" + paramString; } HttpGet httpGet = new HttpGet(url); httpResponse = httpClient.execute(httpGet); } httpEntity = httpResponse.getEntity(); response = EntityUtils.toString(httpEntity); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return response; }
}
--------------AsyncTask For Login------------------
public class Login_Activity extends ActionBarActivity {
//Internet Service NetworkConnection nw; ProgressDialog prgDialog; Boolean netConnection = false; // //Login API String loginURL ="url"; // @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); nw = new NetworkConnection(getApplicationContext()); prgDialog = new ProgressDialog(this); // Set Cancelable as False prgDialog.setCancelable(false); new LoginOperation().execute(); } private class LoginOperation extends AsyncTask { String status, message; @Override protected void onPreExecute() { // Set Progress Dialog Text prgDialog.setMessage("Logging..."); prgDialog.show(); } @Override protected Void doInBackground(String... urls) { if(nw.isConnectingToInternet() == true) { try { List nameValuePairs = new ArrayList(); nameValuePairs.add(new BasicNameValuePair("method", "ClientesLogin")); nameValuePairs.add(new BasicNameValuePair("Email", str_Email)); nameValuePairs.add(new BasicNameValuePair("Senha", str_Password)); ServiceHandler sh = new ServiceHandler(); String response = sh.makeServiceCall(loginURL, ServiceHandler.GET, nameValuePairs); Log.e("response", response); JSONObject js = new JSONObject(response); status = js.getString("status"); Log.e("status",status); if(status.contains("Fail")) { message = js.getString("message"); } /*else { JSONObject jslogin=js.getJSONObject("user_list"); for (int i = 0; i < jslogin.length(); i++) { } }*/ }catch(Exception ex){ } netConnection = true; }else { netConnection = false; } return null; } @Override protected void onPostExecute(Void result) { prgDialog.dismiss(); if(netConnection == false) { Toast toast = Toast.makeText(getApplicationContext(),"Internet is not available. Please turn on and try again.", Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); } else { if(status.contains("Success")) { Toast toast = Toast.makeText(getApplicationContext(), "Login Successful", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); Intent i=new Intent(Login_Activity.this,home_page_activity.class); startActivity(i); } else{ Toast toast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); } } super.onPostExecute(result); } }
}
---------------Network Connection class---------------------
public class NetworkConnection {
Context context; public NetworkConnection(Context context){ this.context = context; } public boolean isConnectingToInternet(){ ConnectivityManager connectivity = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity != null) { NetworkInfo[] info = connectivity.getAllNetworkInfo(); if (info != null) for (int i = 0; i < info.length; i++) if (info[i].getState() == NetworkInfo.State.CONNECTED) { return true; } } return false; }
}
JSONArray main1 = js.getJSONArray("Test 1");
for (int i = 0; i < main1.length(); i++) {
JSONObject jsonObject = main1.getJSONObject(i);
Answer by Seeraj Ahmed for What is the most efficient way on Android to call HTTP Web API calls that return a JSON response?
class Bgtask extends AsyncTask{ JSONObject obj; @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected Void doInBackground(Void... params) { JSONParser parser=new JSONParser(); obj=parser.makeHttpRequest(parser.url); Log.e("onon", obj.toString()); return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); try { JSONObject ob2=obj.getJSONObject("aloha"); JSONArray arr=ob2.getJSONArray("Users"); Log.e("onarr", arr.toString()); Log.e("onon", ob2.toString()); for (int i = 0; i < arr.length(); i++) { JSONObject ob1=arr.getJSONObject(i); Log.e("on", ob1.toString()); DtaItem item=new DtaItem(); item.setFirstName(ob1.getString("first_name")); item.setLastName(ob1.getString("last_name")); item.setGender(ob1.getInt("gender")); item.setAge(ob1.getString("dateofbirth")); itemList.add(item); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } Log.e("size", ""+itemList.size()); ListAdapter adapter=new ListAdapter(MainActivity.this, itemList); list.setAdapter(adapter); } }
Answer by Seeraj Ahmed for What is the most efficient way on Android to call HTTP Web API calls that return a JSON response?
public class JSONParser { static InputStream is = null; static JSONObject jobj = null; static String json = ""; String url="http://162.243.205.148/aloha/apis/searchusers/Server.Rest.ResponseSearchusers.php"; public JSONParser(){ } public JSONObject makeHttpRequest(String url){ JSONObject jo = null; jo = new JSONObject(); try { jo.accumulate("uuid", "2388117894"); jo.accumulate("auth_token", "jNG7QaHFcZKrDt4Y7QOLkp7xsQf1HO"); jo.accumulate("other_uuid", ""); jo.accumulate("keyword", ""); jo.accumulate("timestamp", ""); jo.accumulate("paging", ""); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } JSONObject job=new JSONObject(); try { job.accumulate("aloha", jo); } catch (JSONException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } Log.e("url", job.toString()); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(this.url); try { httppost.setEntity(new StringEntity(job.toString(), "UTF- 8")); // httppost.toString(); // Log.e("url", httppost.toString()); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { HttpResponse httpresponse = httpclient.execute(httppost); HttpEntity httpentity = httpresponse.getEntity(); is = httpentity.getContent(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); StringBuilder sb = new StringBuilder(); String line = null; try { while((line = reader.readLine())!=null){ sb.append(line+"\n"); } is.close(); json = sb.toString(); Log.e("url", json); try { jobj = new JSONObject(json); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return jobj; } package com.example.jasonparser; import java.util.ArrayList; public class ListAdapter extends BaseAdapter{ Context mContext; ArrayList itemList; LayoutInflater inflater; ViewHolder holder; public ListAdapter(Context context,ArrayList itemList) { mContext=context; this.itemList=itemList; inflater=LayoutInflater.from(mContext); holder=new ViewHolder(); } @Override public int getCount() { // TODO Auto-generated method stub return itemList.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return arg0; } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return arg0; } @SuppressLint("ViewHolder") @Override public View getView(int arg0, View arg1, ViewGroup arg2) { arg1=inflater.inflate(R.layout.list_item, null, false); holder=new ViewHolder(); holder.FirstName=(TextView)arg1.findViewById(R.id.firstName); holder.LastName=(TextView)arg1.findViewById(R.id.lastName); holder.Age=(TextView)arg1.findViewById(R.id.age); holder.Gender=(TextView)arg1.findViewById(R.id.gender); holder.FirstName.setText(itemList.get(arg0).getFirstName()); holder.LastName.setText(itemList.get(arg0).getLastName()); holder.Age.setText(itemList.get(arg0).getAge()); holder.Gender.setText(itemList.get(arg0).getGender()); return arg1; } class ViewHolder{ TextView FirstName; TextView LastName; TextView Age; TextView Gender; } } package com.example.jasonparser; public class DtaItem { private String firstName; private String lastName; private String age; private int gender; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getAge() { DateFormat format = new SimpleDateFormat("yyyy-mm-dd", Locale.ENGLISH); Date date = null; try { date = (Date) format.parse(age); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } Calendar dob = Calendar.getInstance(); dob.setTime(date); Calendar today = Calendar.getInstance(); int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR); if (today.get(Calendar.MONTH) < dob.get(Calendar.MONTH)) { age--; } else if (today.get(Calendar.MONTH) == dob.get(Calendar.MONTH) && today.get(Calendar.DAY_OF_MONTH) < dob.get(Calendar.DAY_OF_MONTH)) { age--; } return String.valueOf(age); } public void setAge(String age) { this.age = age; } public String getGender() { if(gender==1) return "Male"; else return "Female"; } public void setGender(int gender) { this.gender = gender; } }
Answer by Seeraj Ahmed for What is the most efficient way on Android to call HTTP Web API calls that return a JSON response?
Link :- http://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient-android/4.3.5.1 public class MainActivity extends Activity { ListView list; ArrayList itemList=new ArrayList(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list=(ListView)findViewById(R.id.list); new Bgtask().execute(); }
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment