Created
June 8, 2015 11:41
-
-
Save yudanta/f2b54292530a43af08bb to your computer and use it in GitHub Desktop.
GetDataFromServer
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
package co.nuwira.nwrandroiddev; | |
import android.content.Intent; | |
import android.os.AsyncTask; | |
import android.os.Bundle; | |
import android.support.v7.app.ActionBarActivity; | |
import android.util.Log; | |
import android.widget.ListView; | |
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; | |
/** | |
* Created by Amanda on 6/8/15. | |
*/ | |
public class GetDataFromServer extends ActionBarActivity{ | |
String response = ""; | |
ListView listData; | |
ArrayList<Shelter> listShelter = new ArrayList<>(); | |
@Override | |
public void onCreate(Bundle savedInstanceState){ | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.list_layout); | |
listData = (ListView) findViewById(R.id.listData); | |
new DataTask().execute(); | |
} | |
private class DataTask extends AsyncTask<Void, Void, Void> { | |
@Override | |
protected void onPreExecute() { | |
} | |
@Override | |
protected Void doInBackground(Void... arg0) { | |
InputStream inputStream = null; | |
try { | |
HttpClient httpClient = new DefaultHttpClient(); | |
HttpResponse httpResponse = httpClient.execute(new HttpGet("http://yudanta.web.id/trans_jogja.json")); | |
inputStream = httpResponse.getEntity().getContent(); | |
if (inputStream != null){ | |
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream)); | |
String line = ""; | |
while((line = bufferedReader.readLine()) != null) | |
response += line; | |
inputStream.close(); | |
} | |
}catch (Exception e){ | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
@Override | |
protected void onPostExecute(Void result) { | |
try { | |
JSONObject jsonData = new JSONObject(response); | |
if (jsonData != null){ | |
if (jsonData.getInt("status") == 200){ | |
listShelter.clear(); | |
JSONArray arrayData = jsonData.getJSONArray("items"); | |
for (int i = 0; i < arrayData.length(); i++) { | |
Shelter newShelter = new Shelter(); | |
newShelter.setIdShelter(arrayData.getJSONObject(i).getInt("id")); | |
newShelter.setName(arrayData.getJSONObject(i).getString("name")); | |
newShelter.setDescription(arrayData.getJSONObject(i).getString("desc")); | |
newShelter.setLatitude(arrayData.getJSONObject(i).getDouble("latitude")); | |
newShelter.setLongitude(arrayData.getJSONObject(i).getDouble("longitude")); | |
listShelter.add(newShelter); | |
} | |
DataAdapter adapter = new DataAdapter(GetDataFromServer.this, R.layout.row_item, listShelter); | |
listData.setAdapter(adapter); | |
listData.invalidate(); | |
} | |
} | |
}catch (JSONException e){ | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment