Skip to content

Instantly share code, notes, and snippets.

@edutrul
Created February 19, 2016 01:36
Show Gist options
  • Save edutrul/aba65ef37ee5dde9293a to your computer and use it in GitHub Desktop.
Save edutrul/aba65ef37ee5dde9293a to your computer and use it in GitHub Desktop.
listview and http request SIMPLE in android
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
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 HttpAsyncTask().execute("http://hmkcode.appspot.com/rest/controller/get.json");
}
public static String GET(String url){
InputStream inputStream = null;
String result = "";
try {
// create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// make GET request to the given URL
HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
// receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// convert inputstream to string
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
return result;
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException {
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
private class HttpAsyncTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... urls) {
return GET(urls[0]);
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
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