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]

Android Listview: clicked item löschen


[java]

package de.clarisweb.android.listview;
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView myListView = findViewById((R.id.myListView));
final ArrayList myFamily = new ArrayList();
myFamily.add("P1");
myFamily.add("P2");
myFamily.add("P3");
myFamily.add("P4");
myFamily.add("P5");
final ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, myFamily);
myListView.setAdapter(arrayAdapter);
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView adapterView, View view, int i, long l) {
Log.i("Person selected: ", myFamily.get(i));
Toast.makeText(MainActivity.this, "Person: " + myFamily.get(i) + " entfernt", Toast.LENGTH_SHORT).show();
arrayAdapter.remove(myFamily.get(i));
arrayAdapter.notifyDataSetChanged(); }
});
}
}

[/java]