我在Android应用程序中创建Slider,因为我使用了Viewpager控件。 我已将所有必需的图像存储在我的数据库的Slider表中。
我使用Node.js创建了REST API,以检索JSON格式的数据。
但实际上我不知道如何在android中调用Node.js RESTAPI来回溯viewpager中的图像。
Server.js File
/*SERVER.JS FILE FOR NODE.JS CODING */
var app = require('express')();
var http = require('http').Server(app);
var mysql = require('mysql');
var bodyParser = require("body-parser");
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'lut',
});
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json()); // Use to retrive data in JSON format
app.get('/',function(req,res){
var data = {
"Data":""
};
data["Data"] = "Welcome to LetUsTEll DEMO...";
res.json(data);
});
//GET images of the slider from database.
app.get('/sliders',function(req,res){
var data = {
"error":1,
"SliderImages":""
};
connection.query("SELECT * from slider",function(err, rows, fields){
if(rows.length != 0){
data["error"] = 0;
data["SliderImages"] = rows;
res.json(data);
}else{
data["SliderImages"] = 'No Slider Image Found..';
res.json(data);
}
});
});
http.listen(8080,function(){
console.log("Connected & Listen to port 8080");
});
Android代码:
private ViewPager intro_images;
private CustomSwipeAdapter mAdapter;
private Handler handler;
private ViewPager intro_images;
private LinearLayout pager_indicator;
private int dotsCount;
private ImageView[] dots;
JSONArray dataCategory;
public CategoryFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
handler = new Handler();
View rootView = inflater.inflate(R.layout.activity_home, container, false);
ActionBar bar = CommonUtil.getActionBar(getActivity());
bar.setTitle("Ask in Bhuj");
FrameLayout fm = (FrameLayout) rootView.findViewById(R.id.fl_category);
/* int height = getResources().getDisplayMetrics().heightPixels;
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) fm.getLayoutParams();
params.height = (int) (height / 4);
fm.setLayoutParams(params);*/
scrollView = (ScrollView) rootView.findViewById(R.id.scrollView1);
new_features = (View) rootView.findViewById(R.id.newfeaturesView);
// ViewPager Adapter to set image
intro_images = (ViewPager) rootView.findViewById(R.id.view_pager);
intro_images.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
Log.d("page changed", String.valueOf(position));
/*try {
// JSONArray productArray = new JSONArray(SharedPreferenceUtil.getString(Constants.PrefKeys.PREF_PROD_LIST, ""));
// ProductGridAdapter.getLastItem();
// Glide.with(CategoryFragment.this).load(Constants.BASE_IMAGE_URL + dataCategory.getJSONObject(position).getString("image_name")).centerCrop().into(imageView);
} catch (JSONException e) {
e.printStackTrace();
}*/
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
// mAdapter = new NewFeatureAdapter(getActivity(), imgresource);
mAdapter = new CustomSwipeAdapter(getActivity(), imgresource);
pager_indicator = (LinearLayout) rootView.findViewById(R.id.viewPagerCountDots);
intro_images.setAdapter(mAdapter);
intro_images.setCurrentItem(0);
intro_images.addOnPageChangeListener(this);
setUiPageViewController();
//For Automatic Slider after 5secs used Handler
handler = new Handler();
runnable = new Runnable() {
public void run() {
if (position >= imgresource.length) {
position = 0;
} else {
position = position + 1;
}
// Setting current Image into viewpager
intro_images.setCurrentItem(position, true);
handler.postDelayed(runnable, 2000);
}
};
//category details in grid view
mGridCategory = (ExpandableHeightGridView) rootView.findViewById(R.id.grid_category);
mGridCategory.setExpanded(true);
/*
mGridCategory.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
scrollView.requestDisallowInterceptTouchEvent(true);
int action = event.getActionMasked();
switch (action) {
case MotionEvent.ACTION_UP:
scrollView.requestDisallowInterceptTouchEvent(false);
break;
}
return false;
}
});
*/
// mGridCategory.setAdapter(new ImageAdapter(getActivity()));
// ImageAdapter adapter = new ImageAdapter(getActivity());
CategoryAdapter adapter = new CategoryAdapter(getActivity());
mGridCategory.setAdapter(adapter);
adapter.notifyDataSetChanged();
mGridCategory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(), "You Clicked at " + position, Toast.LENGTH_SHORT).show();
}
});
return rootView;
}