Advertisement
Guest User

Untitled

a guest
May 18th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.99 KB | None | 0 0
  1. import android.app.ProgressDialog;
  2. import android.content.Intent;
  3. import android.graphics.Bitmap;
  4. import android.graphics.BitmapFactory;
  5. import android.net.Uri;
  6.  
  7. import android.os.AsyncTask;
  8. import android.os.Bundle;
  9. import android.support.design.widget.NavigationView;
  10. import android.support.v4.view.GravityCompat;
  11. import android.support.v4.widget.DrawerLayout;
  12. import android.support.v7.app.ActionBarDrawerToggle;
  13. import android.support.v7.app.AppCompatActivity;
  14. import android.support.v7.widget.Toolbar;
  15. import android.view.Menu;
  16. import android.view.MenuItem;
  17. import android.view.View;
  18. import android.widget.Button;
  19. import android.widget.ImageView;
  20.  
  21. import com.google.android.gms.appindexing.Action;
  22. import com.google.android.gms.appindexing.AppIndex;
  23. import com.google.android.gms.common.api.GoogleApiClient;
  24.  
  25. import org.json.JSONArray;
  26. import org.json.JSONException;
  27. import org.json.JSONObject;
  28.  
  29. import java.io.BufferedReader;
  30. import java.io.IOException;
  31. import java.io.InputStreamReader;
  32. import java.net.HttpURLConnection;
  33. import java.net.MalformedURLException;
  34. import java.net.URL;
  35.  
  36.  
  37. //import com.example.nikos.map.MapsActivity;
  38.  
  39. public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
  40.  
  41. /**
  42. * ATTENTION: This was auto-generated to implement the App Indexing API.
  43. * See https://g.co/AppIndexing/AndroidStudio for more information.
  44. */
  45.  
  46. private String imagesJSON;
  47.  
  48. private static final String JSON_ARRAY ="result";
  49. private static final String IMAGE_URL = "url";
  50. private JSONArray arrayImages= null;
  51. private int TRACK = 0;
  52.  
  53. private static final String IMAGES_URL = "http://192.168.1.116/images.php";
  54.  
  55. private Button buttonFetchImages;
  56. private Button buttonMoveNext;
  57. private Button buttonMovePrevious;
  58. private ImageView imageView;
  59.  
  60.  
  61.  
  62. private GoogleApiClient client;
  63.  
  64. @Override
  65. protected void onCreate(Bundle savedInstanceState) {
  66. super.onCreate(savedInstanceState);
  67. setContentView(R.layout.activity_main);
  68. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  69. setSupportActionBar(toolbar);
  70. imageView = (ImageView) findViewById(R.id.imageView2);
  71.  
  72. buttonMoveNext = (Button) findViewById(R.id.buttonNext);
  73. buttonMovePrevious = (Button) findViewById(R.id.buttonPrev);
  74. getAllImages();
  75.  
  76. /* FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  77. assert fab != null;
  78. fab.setOnClickListener(new View.OnClickListener() {
  79. @Override
  80. public void onClick(View view) {
  81. Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
  82. .setAction("Action", null).show();
  83. }
  84. });
  85. */
  86. DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  87. ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
  88. this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
  89. drawer.setDrawerListener(toggle);
  90. toggle.syncState();
  91.  
  92. NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
  93. navigationView.setNavigationItemSelectedListener(this);
  94. // ATTENTION: This was auto-generated to implement the App Indexing API.
  95. // See https://g.co/AppIndexing/AndroidStudio for more information.
  96. client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
  97.  
  98. }
  99.  
  100. private void extractJSON(){
  101. try {
  102. JSONObject jsonObject = new JSONObject(imagesJSON);
  103. arrayImages = jsonObject.getJSONArray(JSON_ARRAY);
  104. } catch (JSONException e) {
  105. e.printStackTrace();
  106. }
  107. }
  108.  
  109. private void showImage(){
  110. try {
  111. JSONObject jsonObject = arrayImages.getJSONObject(TRACK);
  112. getImage(jsonObject.getString(IMAGE_URL));
  113. } catch (JSONException e) {
  114. e.printStackTrace();
  115. }
  116. }
  117.  
  118. private void moveNext(){
  119. if(TRACK < arrayImages.length()){
  120. TRACK++;
  121. showImage();
  122. }
  123. }
  124.  
  125. private void movePrevious(){
  126. if(TRACK>0){
  127. TRACK--;
  128. showImage();
  129. }
  130. }
  131.  
  132. private void getAllImages() {
  133. class GetAllImages extends AsyncTask<String,Void,String> {
  134. ProgressDialog loading;
  135. @Override
  136. protected void onPreExecute() {
  137. super.onPreExecute();
  138. loading = ProgressDialog.show(MainActivity.this, "Fetching Data...","Please Wait...",true,true);
  139. }
  140.  
  141. @Override
  142. protected void onPostExecute(String s) {
  143. super.onPostExecute(s);
  144. loading.dismiss();
  145. imagesJSON = s;
  146. extractJSON();
  147. showImage();
  148. }
  149.  
  150. @Override
  151. protected String doInBackground(String... params) {
  152. String uri = params[0];
  153. BufferedReader bufferedReader;
  154. try {
  155. URL url = new URL(uri);
  156. HttpURLConnection con = (HttpURLConnection) url.openConnection();
  157. StringBuilder sb = new StringBuilder();
  158.  
  159. bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
  160. String json;
  161. while((json = bufferedReader.readLine())!= null){
  162. sb.append(json+"n");
  163. }
  164.  
  165. return sb.toString().trim();
  166.  
  167. }catch(Exception e){
  168. return null;
  169. }
  170. }
  171. }
  172. GetAllImages gai = new GetAllImages();
  173. gai.execute(IMAGES_URL);
  174. }
  175.  
  176. private void getImage(String urlToImage){
  177. class GetImage extends AsyncTask<String,Void,Bitmap>{
  178. ProgressDialog loading;
  179. @Override
  180. protected Bitmap doInBackground(String... params) {
  181. URL url = null;
  182.  
  183. Bitmap image = null;
  184.  
  185. String urlToImage = params[0];
  186. try {
  187. url = new URL(urlToImage);
  188.  
  189. image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
  190. } catch (MalformedURLException e) {
  191. e.printStackTrace();
  192. } catch (IOException e) {
  193. e.printStackTrace();
  194. }
  195. return image;
  196. }
  197.  
  198. @Override
  199. protected void onPreExecute() {
  200. super.onPreExecute();
  201. loading = ProgressDialog.show(MainActivity.this, "Downloading Image...", "Please wait...", true, true);
  202. }
  203.  
  204. @Override
  205. protected void onPostExecute(Bitmap bitmap) {
  206. super.onPostExecute(bitmap);
  207. loading.dismiss();
  208. imageView.setImageBitmap(bitmap);
  209. }
  210. }
  211. GetImage gi = new GetImage();
  212. gi.execute(urlToImage);
  213. }
  214.  
  215.  
  216. public void onClick(View v) {
  217. if(v == buttonFetchImages) {
  218. getAllImages();
  219. }
  220. if(v == buttonMoveNext){
  221. moveNext();
  222. }
  223. if(v== buttonMovePrevious){
  224. movePrevious();
  225. }
  226. }
  227.  
  228.  
  229.  
  230. @Override
  231. public void onBackPressed() {
  232. DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  233. if (drawer.isDrawerOpen(GravityCompat.START)) {
  234. drawer.closeDrawer(GravityCompat.START);
  235. } else {
  236. super.onBackPressed();
  237. }
  238. }
  239.  
  240. @Override
  241. public boolean onCreateOptionsMenu(Menu menu) {
  242. // Inflate the menu; this adds items to the action bar if it is present.
  243. getMenuInflater().inflate(R.menu.main, menu);
  244. return true;
  245. }
  246.  
  247. @Override
  248. public boolean onOptionsItemSelected(MenuItem item) {
  249. // Handle action bar item clicks here. The action bar will
  250. // automatically handle clicks on the Home/Up button, so long
  251. // as you specify a parent activity in AndroidManifest.xml.
  252. int id = item.getItemId();
  253.  
  254. //noinspection SimplifiableIfStatement
  255. if (id == R.id.action_settings) {
  256. return true;
  257. }
  258.  
  259. return super.onOptionsItemSelected(item);
  260. }
  261.  
  262. @SuppressWarnings("StatementWithEmptyBody")
  263. @Override
  264. public boolean onNavigationItemSelected(MenuItem item) {
  265. // Handle navigation view item clicks here.
  266. int id = item.getItemId();
  267.  
  268. if (id == R.id.login) {
  269. Intent intent3;
  270. intent3 = new Intent(MainActivity.this, LoginActivity.class);
  271. startActivity(intent3);
  272. } else if (id == R.id.map) {
  273. Intent intent2 = new Intent(MainActivity.this, MapsActivity.class);
  274. startActivity(intent2);
  275.  
  276. } else if (id == R.id.search) {
  277. Intent intent6 = new Intent(MainActivity.this, SearchActivity.class);
  278. startActivity(intent6);
  279. } else if (id == R.id.leaf) {
  280. Intent intent5 = new Intent(MainActivity.this, LeafletActivity.class);
  281. startActivity(intent5);
  282. } else if (id == R.id.main) {
  283.  
  284. } else if (id == R.id.information) {
  285. Intent intent4 = new Intent(MainActivity.this, InformationActivity.class);
  286. startActivity(intent4);
  287. } else if (id == R.id.lists) {
  288.  
  289. }
  290.  
  291. DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  292. drawer.closeDrawer(GravityCompat.START);
  293. return true;
  294. }
  295.  
  296. @Override
  297. public void onStart() {
  298. super.onStart();
  299.  
  300. // ATTENTION: This was auto-generated to implement the App Indexing API.
  301. // See https://g.co/AppIndexing/AndroidStudio for more information.
  302. client.connect();
  303. Action viewAction = Action.newAction(
  304. Action.TYPE_VIEW, // TODO: choose an action type.
  305. "Main Page", // TODO: Define a title for the content shown.
  306. // TODO: If you have web page content that matches this app activity's content,
  307. // make sure this auto-generated web page URL is correct.
  308. // Otherwise, set the URL to null.
  309. Uri.parse("http://host/path"),
  310. // TODO: Make sure this auto-generated app deep link URI is correct.
  311. Uri.parse("android-app://com.example.nikos.papaapadakis/http/host/path")
  312. );
  313. AppIndex.AppIndexApi.start(client, viewAction);
  314. }
  315.  
  316. @Override
  317. public void onStop() {
  318. super.onStop();
  319.  
  320. // ATTENTION: This was auto-generated to implement the App Indexing API.
  321. // See https://g.co/AppIndexing/AndroidStudio for more information.
  322. Action viewAction = Action.newAction(
  323. Action.TYPE_VIEW, // TODO: choose an action type.
  324. "Main Page", // TODO: Define a title for the content shown.
  325. // TODO: If you have web page content that matches this app activity's content,
  326. // make sure this auto-generated web page URL is correct.
  327. // Otherwise, set the URL to null.
  328. Uri.parse("http://host/path"),
  329. // TODO: Make sure this auto-generated app deep link URI is correct.
  330. Uri.parse("android-app://com.example.nikos.papaapadakis/http/host/path")
  331. );
  332. AppIndex.AppIndexApi.end(client, viewAction);
  333. client.disconnect();
  334.  
  335. }
  336. }
  337.  
  338. and my php file
  339.  
  340. <?php
  341. $host="localhost"; //replace with database hostname
  342. $username="root"; //replace with database username
  343. $password=""; //replace with database password
  344. $db_name="supermarket"; //replace with database name
  345.  
  346. $con=mysqli_connect("localhost", "root", "")or die("cannot connect");
  347.  
  348.  
  349.  
  350. mysqli_select_db($con,"supermarket")or die("cannot select DB");
  351. $sql = "select pid from deals";
  352.  
  353. $res = mysqli_query($con,$sql);
  354.  
  355.  
  356. $result = array();
  357.  
  358. $url = "http://192.168.1.116/images.php?pid=";
  359. while($row = mysqli_fetch_array($res)){
  360. array_push($result,array('url'=>$url.$row['pid']));
  361. }
  362.  
  363. print json_encode(array("result"=>$result));
  364.  
  365. mysqli_close($con);
  366. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement