Android: get specific JSON attribute

When accessing http://headers.jsontest.com/ the following content is provided:

{
"X-Cloud-Trace-Context": "778d633b22354c11045ece44c646dd2a/11146751159933655926",
"Accept-Language": "de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7,sv;q=0.6,la;q=0.5",
"Host": "headers.jsontest.com",
"DNT": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36",
"Postman-Token": "5851966f-f18b-cb0f-ab9c-e39ca6878d76",
"Accept": "*/*",
"Cache-Control": "no-cache"
}

The Goal is to extract the values for  Host and User-Agent.

first of all make sure that AndroidManifest.xml contains:

uses-permission android:name="android.permission.INTERNET";

Content of MainActivity.java:

package de.clarisweb.android.json_get_specific_attributes;

public class MainActivity extends AppCompatActivity {
public class DownloadTask extends AsyncTask< String, Void, String> {

@Override
protected String doInBackground(String... urls) {

String result = "";
URL url;
HttpURLConnection urlConnection= null;

try {
url = new URL(urls[0]);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data = reader.read();

while (data != -1) {
char current = (char) data;
result += current;
data = reader.read();

}

return result;

} catch (Exception e){

e.printStackTrace();
return null;
}
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);

try {
JSONObject jsonObject = new JSONObject(s);
String hostInfo = jsonObject.getString( "Host");
String userAgent = jsonObject.getString( "User-Agent");
Log.i ("Host : ", hostInfo);
Log.i ("User-Agent : ", userAgent);

}

} catch (Exception e) {
e.printStackTrace();
}

}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

DownloadTask task = new DownloadTask();
task.execute("http://headers.jsontest.com/");

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}
};

extract from Logcat shows:

12-22 16:08:18.303 7903-7903/de.clarisweb.android.json_get_specific_attributes I/Content :: headers.jsontest.com
12-22 16:08:18.303 7903-7903/de.clarisweb.android.json_get_specific_attributes I/Content :: Dalvik/2.1.0 (Linux; U; Android 8.0.0; Android SDK built for x86 Build/OSR1.170901.056)

Android: Query JSON-Data , log the response to logcat


[java]
package de.clarisweb.android.jsondemo;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

public class DownloadTask extends AsyncTask< String, Void, String> {

@Override
protected String doInBackground(String… urls) {

String result = "";
URL url;
HttpURLConnection urlConnection= null;

try {
url = new URL(urls[0]);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data = reader.read();

while (data != -1) {
char current = (char) data;
result += current;
data = reader.read();

}

return result;

} catch (Exception e){

e.printStackTrace();
return null;
}
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);

Log.i ("JSON: ", s);

}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action",Snackbar.LENGTH_LONG)
.setAction("Action", null).show();

}
});

DownloadTask task = new DownloadTask();
//
task.execute("http://headers.jsontest.com/");

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

[/java]

 

Android: Timer

[java]
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

new CountDownTimer(10000, 100) {

public void onTick(long millisecondsUntilDone) {
Log.i("Seconds Left!", String.valueOf(millisecondsUntilDone / 1000));
Toast.makeText(MainActivity.this, "" + String.valueOf(millisecondsUntilDone / 1000), Toast.LENGTH_SHORT).show();
}

public void onFinish() {
Log.i("We’re done!", "No more countdown");
}
}.start();</pre>
[/java]