Created
February 19, 2016 02:17
-
-
Save edutrul/be356a0a41288cb3083a to your computer and use it in GitHub Desktop.
http UrlCOnnection with simplelistview and asynctask
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://hmkcode.appspot.com/rest/controller/get.json"); | |
// System.out.println(getResponseText("http://hmkcode.appspot.com/rest/controller/get.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 { | |
JSONObject json = new JSONObject(result); // convert String to JSONObject | |
JSONArray articles = json.getJSONArray("articleList"); // get articles array | |
String titulos[]; | |
titulos = new String[articles.length()]; | |
for (int i = 0; i < articles.length(); i++) { | |
System.out.println(articles.getJSONObject(i).get("title")); | |
titulos[i] = articles.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