Query CSV File using Java RMI Client/Server

by

 

This application working on Java RMI, to Query CSV file through the network.
the client will send the query to the server, the code will be executed on the server.
the server will use Apache Commons CSV  to read the CSV file.


the result of the query will be streamed to the client through the network.
the client will read the result and display it to the end user.

 

Snapshot of the result(Client).


I will record youtube video, how to run the server and the client.
this example just to show you the concepts of Java RMI.

 

Code gitHub

JSOUP with Web Crawler

by

Hi, the example for today is how to use JSOUP library with Web Crawler.

the next java code use thread pool to generate thread to search for keywords you have been provided on the selected website.

JSOUP will do the connection to the website, download the page content (HTML), search for keywords and at the same time will save copy form all the links have been found in the current page.

ThreadPool will generate thread to make the search faster.

Snapshot Form Final Result :

 

 

Source code:

https://github.com/Alaeddinalhamoud/Web-Crawler-JAVA

 

How to Use Twitter4J

by

Today, I going to explain about Twitter4J.

Twitter4J is an unofficial Java library for the Twitter API.
With Twitter4J, you can easily integrate your Java application with the Twitter service. Twitter4J is an unofficial library.

Twitter4J is featuring: 
✔ 100% Pure Java - works on any Java Platform version 5 or later
✔ Android platform and Google App Engine ready
✔ Zero dependencies: No additional jars required
✔ Built-in OAuth support
✔ Out-of-the-box gzip support
✔ 100% Twitter API 1.1 compatible

I will explain using the following code:

Java Application uses Twitter4J to work with Twitter API.

the Result of the application: just after you enter the Twitter @UseName, you want to know about it. The following...

1-Number Followed. 2-Number Of Tweets Received. 3-Number Of Retweets. 4-Most Active Followed.

There are too many methods you can call to analysis Twitter Account.

Note:

  • Do not forget the add your twitter app key.
  • Import Twitter4J API to your project (Included in Lib Folder).

 

 

  import java.util.ArrayList;
  import java.util.Date;
  import java.util.HashMap;
  import java.util.LinkedList;
  import java.util.List;
  import java.util.Map;
  import java.util.Map.Entry;
  import java.util.Scanner;
   
   
   
  import twitter4j.PagableResponseList;
  import twitter4j.Paging;
  import twitter4j.Status;
  import twitter4j.TwitterException;
  import twitter4j.TwitterFactory;
  import twitter4j.User;
  import twitter4j.conf.ConfigurationBuilder;
   
  public class Main {
   
  static ConfigurationBuilder _ConfigurationBuilder;// Config builder to build the connection for twitter api
  static TwitterFactory _TwitterFactory;//Building the factory
  static twitter4j.Twitter twitter;// Twitter4j lib to get the data from twitter
  static long coursor = -1;//used to read multi page
  static PagableResponseList<User> _User;// number of follower of the number of friends of account
  static HashMap<String, Integer> ListofFollower;//List of follower to save all the follower to using them in comparing
  static HashMap<String, Integer> userstweets;
  static List<Long> datestatus;// to save number of retweets during date
  static String UserName;// the username, we got it from the soa servies link
  static int NumberOfReTweets;
  static int TotalNumberOfRetweet;
  static int TotalNumberOfFollwed;
  static String MostActiveFollowed;
  static int TotalNumberOfTweetsReceived;
   
  public static void main(String[] args) {
  // TODO Auto-generated method stub
  // Initional Twitter API
  Init();// Use Your Key
  TweetAnalysis();
  }
   
  public static void GetUserName() {
   
  Scanner scanner = new Scanner(System.in);
   
  System.out.print("Enter a Twitter UserName:");
  UserName= scanner.nextLine();
  System.out.println(UserName);
   
  }
   
  public static void print(String arg1,String arg2,String arg3) {
  System.out.println(arg1+ " "+arg2+" "+arg3);
  }
   
  //Main method will be called many time to execute all the following methods
  public static void TweetAnalysis() {
  // To Get the UserName from Restful Link and save it on name.
  GetUserName();
  // If UserName gives twitter name/@aladinhamod.....etc../.
  if (!UserName.isEmpty()) {
  // This Method Will give Number Of Followed And Most Active Followed And NumOfTweetsReceived
  GetNumOfFollowedMostActiveFollowedAndNumOfTweetsReceived(UserName);
  //Number Of Retweets During Jan 2018
  GetNumberOfReTweet(UserName);
  // NumberFollowed
  print("NumberFollowed", UserName, String.valueOf(TotalNumberOfFollwed));
  // MostActiveFollowed
  print("MostActiveFollowed", UserName, MostActiveFollowed);
  // NumberOfRetweets
  print("NumberOfRetweets", UserName, String.valueOf(NumberOfReTweets));
  // NumberOfTweetsReceived
  print("NumberOfTweetsReceived", UserName, String.valueOf(TotalNumberOfTweetsReceived));
  } else {
  System.out.println("Please Check Your Internet Connection");
  }
  }
   
  // Twitter API Will Return Number of NumOfFollowedMostActiveFollowedAndNumOfTweetsReceived
  public static void GetNumOfFollowedMostActiveFollowedAndNumOfTweetsReceived(String _UserName) {
  //HashMap to Save Number of Users
  ListofFollower = new HashMap<String, Integer>();
  TotalNumberOfTweetsReceived=0;
   
  do {
  try {
  //To get the number of followed
  _User = twitter.getFriendsList(_UserName, coursor);
  } catch (TwitterException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  }
  //Loop to Read the data from followed list
  //to get Name of Best user
  for (User floower : _User) {
  String username = floower.getScreenName();// Friend Name ,, follower name
  int flooercout = floower.getStatusesCount(); /// Follower status count
  //Save the date to Hashmap
  ListofFollower.put(username, flooercout);
  //Get the TotalNumberOfTweetsReceived is total of user status
  TotalNumberOfTweetsReceived += flooercout;
  }
  //Contionus to finish data (maybe save in more that page)
  } while ((coursor = _User.getNextCursor()) != 0);
  //To get the active user from the followed list
  Map.Entry<String, Integer> maxEntry = null;
  TotalNumberOfFollwed = ListofFollower.size();
  System.out.println("Tolal Number of Followed : " + TotalNumberOfFollwed);// Question 1
  //Comparing the tweets for the users to get the active follwer.
  for (Entry<String, Integer> entry : ListofFollower.entrySet()) {
   
  if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0) {
  maxEntry = entry;
  }
  }
  //Getting the date from the comparing
  MostActiveFollowed = maxEntry.getKey();
  int NumberofTweet = maxEntry.getValue();
  System.out.println("Most Active Followed : " + MostActiveFollowed + " By Tweets number : " + NumberofTweet); // Question 4
  System.out.println("Total Number Of Tweets Received : " + TotalNumberOfTweetsReceived);//Qestion 2
  }
  //Get the number of retweet by jan 2018
  public static void GetNumberOfReTweet(String _UserName) {
  //the first page
  int pageno = 1;
  String user = _UserName;
  //Save the list of tweets by date
  datestatus = new LinkedList<>();
  //Date format to get he month MM and the Year YYYY
  SimpleDateFormat sdf = new SimpleDateFormat("MM/YYYY");
  //TO save the status of user
  List<Status> statuses = new ArrayList<Status>();
  //Loop to read maybe more than 1 page
  while (true) {
   
  try {
   
  int size = statuses.size();
  Paging page = new Paging(pageno++, 200);
  //get all the user time line and save it ti statues
  statuses.addAll(twitter.getUserTimeline(user, page));
  //read the status one by one and get the retweets filterd by date
  for (Status s : statuses) {
  Date date = s.getCreatedAt();
  String dt = sdf.format(date);
  boolean is = s.isRetweet();
  Long id = s.getId();
  String str = "01/2018";
  if (!datestatus.contains(id)) {
  if ((dt.equals(str)) && (is)) {
  datestatus.add(id);
  }
  }
  }
   
  if (statuses.size() == size)
  break;
  } catch (TwitterException e) {
   
  e.printStackTrace();
  }
  }
  //Numebr of retweets by date
  NumberOfReTweets = datestatus.size();// Question .3
  System.out.println("Total Tweets Number Of ReTweets during Jan 2018 : " + NumberOfReTweets);
   
  }
   
   
  //This method to preper my twitter api and build the connection with twitter using twitter4j
  public static void Init() {
  _ConfigurationBuilder = new ConfigurationBuilder();
  _ConfigurationBuilder.setDebugEnabled(true).setOAuthConsumerKey("Use Your Key")
  .setOAuthConsumerSecret("Use Your Key")
  .setOAuthAccessToken("Use Your Key")
  .setOAuthAccessTokenSecret("Use Your Key");
  _TwitterFactory = new TwitterFactory(_ConfigurationBuilder.build());
  twitter = _TwitterFactory.getInstance();
  }
   
  }

 

 

Source: Twitter4J

 

https://github.com/Alaeddinalhamoud/TweetAnalysis-Twitter4J

List View - Android Part 1

by

Android ListView is a view which groups several items and display them in vertical scrollable list.

The list items are automatically inserted to the list using an Adapter.

 that pulls content from a source such as an array or database.

An adapter actually bridges between UI components and the data source that fill data into UI Component. Adapter holds the data and send the data to adapter view, the view can takes the data from adapter view and shows the data on different views like as spinner, list view, grid view etc.

The ListView and GridView are subclasses of AdapterView and they can be populated by binding them to an Adapter, which retrieves data from an external source and creates a View that represents each data entry.

Android provides several subclasses of Adapter that are useful for retrieving different kinds of data and building views for an AdapterView ( i.e. ListView or GridView). The common adapters are ArrayAdapter,Base Adapter,CursorAdapter, SimpleCursorAdapter,SpinnerAdapter and WrapperListAdapter. We will see separate examples for both the adapters.

ArrayAdapter

You can use this adapter when your data source is an array. By default, ArrayAdapter creates a view for each array item by calling toString() on each item and placing the contents in a TextView. Consider you have an array of strings you want to display in a ListView, initialize a new ArrayAdapter using a constructor to specify the layout for each string and the string array −

ArrayAdapter adapter = new ArrayAdapter<String>(this,R.layout.ListView,StringArray);

Here are arguments for this constructor −

  • First argument this is the application context. Most of the case, keep it this.

  • Second argument will be layout defined in XML file and having TextView for each string in the array.

  • Final argument is an array of strings which will be populated in the text view.

Once you have array adapter created, then simply call setAdapter() on your ListView object as follows −

ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);

You will define your list view under res/layout directory in an XML file. For our example we are going to using activity_main.xml file.

Example

Following is the example which will take you through simple steps to show how to create your own Android application using ListView. Follow the following steps to modify the Android application we created in Hello World Example chapter −

 

Following is the content of the modified main activity file src/com.example.ListDisplay/ListDisplay.java. This file can include each of the fundamental life cycle methods.

package com.example.ListDisplay;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ListDisplay extends Activity {
   // Array of strings...
   String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry",
      "WebOS","Ubuntu","Windows7","Max OS X"};
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      ArrayAdapter adapter = new ArrayAdapter<String>(this,
         R.layout.activity_listview, mobileArray);
      ListView listView = (ListView) findViewById(R.id.mobile_list);
      listView.setAdapter(adapter);
   }
}

Following will be the content of res/layout/activity_main.xml file −

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   tools:context=".ListActivity" >
   <ListView
      android:id="@+id/mobile_list"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" >
   </ListView>
</LinearLayout>
Following will be the content of res/values/strings.xml to define two new constants −
<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">ListDisplay</string>
   <string name="action_settings">Settings</string>
</resources>
Following will be the content of res/layout/activity_listview.xml file −
<?xml version="1.0" encoding="utf-8"?>
<!--  Single List Item Design -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/label"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:padding="10dip"
   android:textSize="16dip"
   android:textStyle="bold" >
</TextView>

Let's try to run our modified Hello World! application we just modified. I assume you had created your AVD while doing environment set-up. To run the app from Android studio, open one of your project's activity files and click Run Eclipse Run Icon icon from the tool bar. Android studio installs the app on your AVD and starts it and if everything is fine with your set-up and application, it will display following Emulator window −

 

 

 

Youtube On Android App part 1

by

السلام عليكم ورحمة الله بركاتة , 

في المقالة التاليه سوف نتحدث عن امكانية تشغيل فيديو Youtube  في برنامج Android  .

 سنقوم باستخدام Google API  التى تعطينا ميزات كثيره منها Youtube APIs  .

كما في الصوره التالية يتم انشاء مشؤوع على منصة Google API Manager 

رابط الكونسول Google API's  , 

 

نقوم بالنقر على السهم المحدد باللون الاصفر من اجل انشاء مشروع جديد على  Google API  , 

بعد ذلك نحتاج للذهاب إلى تبويب  Library  ,  من اجل تفعيل Youtube Data API

قم بعمل التالي  Youtube Data API = Enable

الخطوة التالية , من Credentials Ta  قم بانشاء مفتاح API key   .

 

الخطوة التالية , وهي اعطاء او تخصيص المفتاح لمنصه معينه (ممكن ان تكون none ), لكن للحماية الاكبر , اختر Android APPS  .

وقم باضافة Package Name  and SH-1

?? ??? .

يتبع ....