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)

Comments (1)

  1. ugu5ma

    in case you want to display the data on the smartphone you need to create a textview and paste the following lines to the code (under the Log.i … statement:

    TextView textview=(TextView) findViewById(R.id.textView);
    textview.setText(“Host-Info: “+hostInfo+”\n”+”\n”+”User-Agent: “+userAgent);

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.