Last active
February 20, 2016 01:29
-
-
Save edutrul/48a8eb4ea293069a09f5 to your computer and use it in GitHub Desktop.
REST GET from android to DRUPAL with a simple LISTVIEW
This file contains 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
import android.os.AsyncTask; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.widget.ArrayAdapter; | |
import android.widget.ListView; | |
import android.widget.Toast; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import java.io.BufferedInputStream; | |
import java.io.BufferedReader; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
public class MainActivity extends AppCompatActivity { | |
ListView listado; | |
public static String [] nombres = { | |
"Let Us C", | |
"c++", | |
"JAVA", | |
"Jsp", | |
"Microsoft .Net", | |
"Let Us C", | |
"c++", | |
"JAVA", | |
"Jsp", | |
"Microsoft .Net", | |
"Microsoft .Net", | |
"Microsoft .Net", | |
}; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// call AsynTask to perform network operation on separate thread | |
new getData().execute("http://dev-edutrul.pantheon.io/ws/eventos-ws.json"); | |
} | |
public class getData extends AsyncTask<String, String, String> { | |
HttpURLConnection urlConnection; | |
@Override | |
protected String doInBackground(String... args) { | |
StringBuilder result = new StringBuilder(); | |
try { | |
URL url = new URL(args[0]); | |
urlConnection = (HttpURLConnection) url.openConnection(); | |
InputStream in = new BufferedInputStream(urlConnection.getInputStream()); | |
BufferedReader reader = new BufferedReader(new InputStreamReader(in)); | |
String line; | |
while ((line = reader.readLine()) != null) { | |
result.append(line); | |
} | |
}catch( Exception e) { | |
e.printStackTrace(); | |
} | |
finally { | |
urlConnection.disconnect(); | |
} | |
return result.toString(); | |
} | |
@Override | |
protected void onPostExecute(String result) { | |
//Do something with the JSON string | |
Toast.makeText(getBaseContext(), "Received!", Toast.LENGTH_LONG).show(); | |
System.out.println(result); | |
try { | |
JSONArray contenidos = new JSONArray(result); // get articles array | |
System.out.println(contenidos); | |
String titulos[]; | |
titulos = new String[contenidos.length()]; | |
for (int i = 0; i < contenidos.length(); i++) { | |
System.out.println(contenidos.getJSONObject(i).get("title")); | |
titulos[i] = contenidos.getJSONObject(i).get("title").toString(); | |
} | |
listado=(ListView) findViewById(R.id.listado); | |
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, | |
android.R.layout.simple_list_item_1, titulos); | |
listado.setAdapter(adapter); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment