Skip to content

Instantly share code, notes, and snippets.

@pchettiy
Last active April 10, 2016 17:39
Show Gist options
  • Save pchettiy/162d8a5a057a22bc75277f0c2b543c4b to your computer and use it in GitHub Desktop.
Save pchettiy/162d8a5a057a22bc75277f0c2b543c4b to your computer and use it in GitHub Desktop.
This gist contains boiler plate code which you can copy paste evertime you want to do the given operations
Getting server data :
public class AsyncgetTopics extends AsyncTask<String,Void,String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
bar.setVisibility(View.VISIBLE);
//pd= ProgressDialog.show(Activity.this,"Getting data","Please wait..",true,false);
}
@Override
protected String doInBackground(String... params ) {
String result ="";
int status = 0;
String downloadurl=getResources().getString(R.string.BASE_URL)+"/route";
try{
URL url=new URL(downloadurl);
try{
HttpURLConnection urlConnection= (HttpURLConnection) url.openConnection();
String postParameters=
"param0="+params[0]
+"&param1="+params[1];
// USING POST HERE
// To use GET ,change the url appropriately and add params directly there.
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
urlConnection.setFixedLengthStreamingMode(
postParameters.getBytes().length);
PrintWriter out = new PrintWriter(urlConnection.getOutputStream());
out.print(postParameters);
out.close();
InputStream inputStream=urlConnection.getInputStream();
status=urlConnection.getResponseCode();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
String line="";
while((line=bufferedReader.readLine())!=null){
Log.d("line",line);
result+=line;
}
try {
JSONObject object=new JSONObject(result);
JSONArray array=object.getJSONArray("topics");
for(int i=0;i<array.length();i++){
topicList.add(array.getString(i));
Log.d("list",array.getString(i));
}
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("DATA",result);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
//return topiclist;
return String.valueOf( status);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
bar.setVisibility(View.GONE);
//pd.dismiss();
}
}
Setting up viewPager :
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(Frag.newInstance("something"), "Tab1");
adapter.addFragment(Frag.newInstance("somethingelse"), "Tab2");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
SharedPreferences:
onCreate(){
SharedPreferences prefs = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
}
public void save(Context context, String key, String value) {
SharedPreferences.Editor editor = prefs.edit();
editor.putString(key, value);
editor.commit();
}
public void get(String key, String defaultValue){
sharedPreferences=getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
String picurl=sharedPreferences.getString(key,defaultValue);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment