Android basic training networking

Содержание

Слайд 2

Networking. Premissions "android.permission.ACCESS_NETWORK_STATE" />

Networking. Premissions


"android.permission.ACCESS_NETWORK_STATE" />

Слайд 3

Networking. Manage Network Connection final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); final

Networking. Manage Network Connection

final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo networkInfo

= connectivityManager.getActiveNetworkInfo();
if (networkInfo != null) {
//no internet connection
}
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI && networkInfo.isConnected()) {
//connected to wi-fi
} else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE && networkInfo.isConnected()) {
//connected to wi-fi }
Слайд 4

Networking. Connectivity Change 1 Explicit boadcast: final IntentFilter filter = new

Networking. Connectivity Change

1 Explicit boadcast:
final IntentFilter filter = new IntentFilter();

filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
registerReceiver(new ConnectivityReceiver(), filter);
2 Job Scheduling
3 GcmNetworkManager
Слайд 5

Networking. Connectivity Change Job Scheduler: JobScheduler js = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); JobInfo

Networking. Connectivity Change

Job Scheduler:
JobScheduler js = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
JobInfo job = new

JobInfo.Builder(MY_BACKGROUND_JOB,
new ComponentName(context, MyJobService.class))
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
.setRequiresCharging(true)
.build();
js.schedule(job);
Слайд 6

Networking. Data Usage 1 Detect if connection is metered or unmetered

Networking. Data Usage

1 Detect if connection is metered or unmetered
2 Detect

if Data Saver is on and app is in whitelist
3 Use PreferenceActivity to handle data usage in your app
Слайд 7

Networking. Connecting final URL url = new URL("exampleurl.com"); final HttpsURLConnection connection

Networking. Connecting

final URL url = new URL("exampleurl.com");
final HttpsURLConnection connection = (HttpsURLConnection)

url.openConnection();
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.connect();
final int resonseCode = connection.getResponseCode();
if (resonseCode != HttpsURLConnection.HTTP_OK) {
//handle an error, throw exception
}
inputStream = connection.getInputStream();
if (inputStream != null) {
//read response
}
Слайд 8

Networking. Read Input Stream if (inputStream != null) { StringBuffer stringBuffer

Networking. Read Input Stream

if (inputStream != null) {
StringBuffer stringBuffer = new

StringBuffer();
try {
inputStream = new BufferedInputStream(connection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String inputLine = "";
while ((inputLine = reader.readLine()) != null) {
stringBuffer.append(inputLine);
}
result = stringBuffer.toString();
}
catch (Exception e) {
//handle an exception
} finally { //close inputStream }
Слайд 9

Networking. JSON Parsing { "users": [ { "id": "007", "name": "James

Networking. JSON Parsing

{
"users": [
{
"id": "007",
"name": "James Bond",

"email": "james.bond@MI6.com",
"address": "London",
"gender": "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000"
},
"weapon": "Walther PPK"
},
] }
Слайд 10

Networking. JSON Parsing final JSONObject object = new JSONObject(result); final JSONArray

Networking. JSON Parsing

final JSONObject object = new JSONObject(result);
final JSONArray jsonArray =

object.getJSONArray("users");
if (jsonArray != null && jsonArray.length() > 0) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject userObj = jsonArray.getJSONObject(i);
User user = new User();
user.name = userObj.getString("name");
user.email = userObj.getString("email");
user.address = userObj.getString("address");
user.gender = userObj.getString("gender");
user.weapon = userObj.getString("weapon");
JSONObject phoneObj = userObj.getJSONObject("phone");
Phone phone = new Phone();
phone.mobile = phoneObj.getString("mobile");
phone.home = phoneObj.getString("home");
user.phone = phone; }}
Слайд 11

Networking. JSON Parsing Google GSON https://github.com/google/gson final Gson gson = new

Networking. JSON Parsing

Google GSON
https://github.com/google/gson
final Gson gson = new GsonBuilder().create();
final User[]

users = gson.fromJson(reader, User[].class);
Слайд 12

Networking. UI thread

Networking. UI thread