Note that there are some explanatory texts on larger screens.

plurals
  1. POConvert android AsyncTask call to a separate class and call from all activities
    primarykey
    data
    text
    <p>I am new to android development. I have a AsyncTask function in my application. Calling http request from all activities. Now in each activity I am using the following class to connect to server, in some activities I even called twice !!.</p> <p>Basically I am a web developer and in such cases we use a single class which can be accessed from entire application(web) and use the common function to do the same activity. The only difference is input and out put will be changed.</p> <p>My doubt is in this case can I use ( convert) this to such a function or class ? My assume is </p> <ol> <li>Create an android class ( which can be accessed from all the activities )</li> <li>Just make the JSON string we need with specific server ( for process in server )</li> <li>Just pass the created json to the created class and then made the http connect )</li> <li>Process the returned data from server</li> <li>Pass that to the corresponding activity</li> </ol> <p>So that I can use the same function for all the activities and I can avoid duplicate query</p> <p>Can I convert this code to such a manner ?</p> <p><strong>My Code</strong></p> <pre><code>public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); setContentView(R.layout.activity_main); LogIN loginUser = new LogIN(); LoginUser.execute(""); } private class LogIN extends AsyncTask&lt;String, Integer, String&gt; { @Override protected String doInBackground(String... sUrl) { try { String path = "http://www.domain_name.com/app/checkSession.php"; HttpClient client = new DefaultHttpClient(); HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); HttpResponse response; JSONObject json = new JSONObject(); try { HttpPost post = new HttpPost(path); json.put("access_token", "123456"); post.setHeader("json", json.toString()); StringEntity se = new StringEntity(json.toString()); se.setContentEncoding((Header) new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); post.setEntity(se); response = client.execute(post); /* Checking response */ if (response != null) { InputStream in = response.getEntity().getContent(); String a = convertStreamToString(in); JSONObject jsono = stringToJsonobj(a); String passedStringValue = jsono.getString("result"); if(passedStringValue.equals("1")){ flags=1; //Log.v("TAGG", "Success"); } else { flags=0; //Log.v("TAGG", "Failed !"); } } } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { } return null; } @Override protected void onPreExecute() { super.onPreExecute(); showDialogue("Login Processing", "Loading"); } @Override protected void onProgressUpdate(Integer... progress) { super.onProgressUpdate(progress); } @Override protected void onPostExecute(String result) { if(flags.equals(1)){ Itent homepage = new Intent(MainActivity.this, RegisterDevice.class); startActivity(homepage); finish(); } else { Intent homepage = new Intent(MainActivity.this, LoginActivity.class); startActivity(homepage); finish(); } super.onPostExecute(result); } } } </code></pre> <p>Please any one help/advise Thanks in advance</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload