Created
May 24, 2015 17:30
-
-
Save piatra/63df610cfe37e41ebe03 to your computer and use it in GitHub Desktop.
lab practic 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
VICTOR ANDREI OPREA 341 C1 | |
*/ | |
package practicaltest02.pdsd.systems.cs.pub.ro.practicaltest02; | |
import android.support.v7.app.ActionBarActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.TextView; | |
import org.json.JSONObject; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.PrintWriter; | |
import java.net.HttpURLConnection; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
import java.net.URL; | |
class Utilities { | |
public static BufferedReader getReader(Socket socket) throws IOException { | |
try { | |
return new BufferedReader(new InputStreamReader(socket.getInputStream())); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
public static PrintWriter getWriter(Socket socket) throws IOException { | |
return new PrintWriter(socket.getOutputStream(), true); | |
} | |
} | |
public class PracticalTest02MainActivity extends ActionBarActivity { | |
TextView infoText; | |
ServerThread serverThread; | |
Button startButton; | |
Button allButton; | |
int port = 9001; | |
String country; | |
String city; | |
class StartServerButtonListener implements Button.OnClickListener { | |
@Override | |
public void onClick(View v) { | |
Log.d("CLICK", "STARTING SERVER"); | |
serverThread = new ServerThread(); | |
serverThread.startServer(port); | |
} | |
} | |
class SendButtonListener implements Button.OnClickListener { | |
@Override | |
public void onClick(View v) { | |
Button b = (Button) v; | |
infoText.setText(""); | |
ClientThread clientThread = new ClientThread(); | |
country = ((EditText) findViewById(R.id.country)).getText().toString(); | |
city = ((EditText) findViewById(R.id.city)).getText().toString(); | |
clientThread.startClient(b.getText().toString()); | |
} | |
} | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_practical_test02_main); | |
infoText = (TextView) findViewById(R.id.weather_forecast_text_view); | |
startButton = (Button) findViewById(R.id.connect_button); | |
allButton = (Button) findViewById(R.id.get_weather_forecast_button); | |
startButton.setOnClickListener(new StartServerButtonListener()); | |
allButton.setOnClickListener(new SendButtonListener()); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.menu_practical_test02_main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
//noinspection SimplifiableIfStatement | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
private class ClientThread extends Thread { | |
private Socket socket = null; | |
private String info = ""; | |
public void startClient(String info) { | |
this.info = info; | |
start(); | |
} | |
@Override | |
public void run() { | |
try { | |
Socket socket = new Socket("127.0.0.1", port); | |
PrintWriter writer = Utilities.getWriter(socket); | |
writer.println(info); | |
writer.flush(); | |
BufferedReader reader = Utilities.getReader(socket); | |
while(true) { | |
final String line = reader.readLine(); | |
if (line.equals("end")) | |
break; | |
infoText.post(new Runnable() { | |
@Override | |
public void run() { | |
String text = infoText.getText().toString(); | |
infoText.setText(text + "\n" + line); | |
} | |
}); | |
} | |
socket.close(); | |
} catch (Exception exception) { | |
exception.printStackTrace(); | |
} | |
} | |
} | |
private class ServerThread extends Thread { | |
private boolean isRunning = false; | |
int port = 8000; | |
private ServerSocket serverSocket; | |
private Meteo cache; | |
public void startServer(int p) { | |
this.port = p; | |
cache = new Meteo("empty", "empty", "empty"); | |
isRunning = true; | |
start(); | |
} | |
public void stopServer() { | |
isRunning = false; | |
new Thread(new Runnable() { | |
@Override | |
public void run() { | |
try { | |
if (serverSocket != null) { | |
serverSocket.close(); | |
} | |
} catch(IOException ioException) { | |
ioException.printStackTrace(); | |
} | |
} | |
}).start(); | |
} | |
public void run() { | |
try { | |
serverSocket = new ServerSocket(this.port); | |
while (isRunning) { | |
Socket socket = serverSocket.accept(); | |
if (cache == null || !cache.country.equals(country)) { | |
String url = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "," + country; | |
URL obj = new URL(url); | |
HttpURLConnection con = (HttpURLConnection) obj.openConnection(); | |
// optional default is GET | |
con.setRequestMethod("GET"); | |
int responseCode = con.getResponseCode(); | |
System.out.println("\nSending 'GET' request to URL : " + url); | |
System.out.println("Response Code : " + responseCode); | |
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); | |
String inputLine; | |
StringBuffer response = new StringBuffer(); | |
while ((inputLine = in.readLine()) != null) { | |
response.append(inputLine); | |
} | |
in.close(); | |
//print result | |
System.out.println(response.toString()); | |
try { | |
JSONObject json = new JSONObject(response.toString()); | |
cache = new Meteo(json.getJSONObject("main").getString("temp"), | |
json.getJSONObject("main").getString("humidity"), | |
country); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
Log.d("Server", "NULL response from endpoint"); | |
} | |
} | |
BufferedReader reader = Utilities.getReader(socket); | |
String option = reader.readLine(); | |
Log.d("OPTION", option); | |
if (cache != null) { | |
Log.d("CACHE", cache.temperature); | |
Log.d("CACHE", cache.humidity); | |
} | |
if (option != null && cache != null) { | |
PrintWriter writer = Utilities.getWriter(socket); | |
if (option.compareTo("get_weather_forecast") == 0) { | |
writer.println(cache.temperature); | |
writer.println(cache.humidity); | |
} | |
if (option.compareTo("Temperature") == 0) { | |
writer.println(cache.temperature); | |
} | |
if (option.compareTo("Humidity") == 0) { | |
writer.println(cache.humidity); | |
} | |
writer.println("end"); | |
} | |
socket.close(); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
Log.d("SERVER", e.getMessage()); | |
} | |
} | |
} | |
private class Meteo { | |
public String temperature; | |
public String humidity; | |
public String country; | |
public Meteo(String t, String h, String c) { | |
this.temperature = t; | |
this.humidity = h; | |
this.country = c; | |
} | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
android:orientation="vertical" | |
tools:context="ro.pub.cs.systems.pdsd.practicaltest02.graphicuserinterface.PracticalTest02MainActivity" > | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center" | |
android:textSize="25sp" | |
android:textStyle="bold" | |
android:text="server" /> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:baselineAligned="false"> | |
<ScrollView | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="1"> | |
<EditText | |
android:id="@+id/server_port_edit_text" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="server_port" /> | |
</ScrollView> | |
<ScrollView | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="1"> | |
<Button | |
android:id="@+id/connect_button" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center" | |
android:text="connect" /> | |
</ScrollView> | |
</LinearLayout> | |
<Space | |
android:layout_width="wrap_content" | |
android:layout_height="10dp" /> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center" | |
android:textSize="25sp" | |
android:textStyle="bold" | |
android:text="client" /> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:baselineAligned="false"> | |
<ScrollView | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="1"> | |
<EditText | |
android:id="@+id/city" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="city" /> | |
</ScrollView> | |
<ScrollView | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="1"> | |
<EditText | |
android:id="@+id/country" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="country" /> | |
</ScrollView> | |
</LinearLayout> | |
<GridLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:rowCount="2" | |
android:columnCount="2"> | |
<Button | |
android:id="@+id/get_weather_forecast_button" | |
android:text="get_weather_forecast" /> | |
<Spinner | |
android:id="@+id/information_type_spinner" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:entries="@array/information_types" | |
android:layout_row="1" | |
android:layout_column="0" /> | |
</GridLayout> | |
<ScrollView | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<TextView | |
android:id="@+id/weather_forecast_text_view" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:singleLine="false" | |
android:maxLines="10" /> | |
</ScrollView> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="practicaltest02.pdsd.systems.cs.pub.ro.practicaltest02" > | |
<uses-permission android:name="android.permission.INTERNET"/> | |
<application | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:theme="@style/AppTheme" > | |
<activity | |
android:name=".PracticalTest02MainActivity" | |
android:label="@string/app_name" > | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment