--- /dev/null
+package jp.ac.titech.sharp4k.cuten;
+
+import java.io.IOException;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.util.EntityUtils;
+import android.net.ParseException;
+import android.os.AsyncTask;
+import android.util.Log;
+
+public class DownloadTask extends AsyncTask<String, Void, String> {
+ JsonParseTestActivity main;
+
+ public DownloadTask(JsonParseTestActivity main) {
+ this.main = main;
+ }
+
+ @Override
+ protected String doInBackground(String... uri) {
+ HttpClient httpClient = new DefaultHttpClient();
+ HttpUriRequest httpRequest = new HttpGet(uri[0]);
+ HttpResponse httpResponse = null;
+ try {
+ httpResponse = httpClient.execute(httpRequest);
+ } catch (ClientProtocolException e) {
+ Log.e("exception", "ClientProtocolException");
+ e.printStackTrace();
+ } catch (IOException e) {
+ Log.e("exception", "IOException");
+ e.printStackTrace();
+ }
+ String json = null;
+ if (httpResponse != null
+ && httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
+ HttpEntity httpEntity = httpResponse.getEntity();
+ try {
+ json = EntityUtils.toString(httpEntity);
+ } catch (ParseException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ try {
+ httpEntity.consumeContent();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ httpClient.getConnectionManager().shutdown();
+ return json;
+ }
+
+ @Override
+ protected void onPostExecute(String result) {
+ main.jsonParse(result);
+ main.classifyData();
+ main.configListView();
+ }
+
+}
package jp.ac.titech.sharp4k.cuten;
-import java.io.IOException;
-import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
-
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpResponse;
-import org.apache.http.HttpStatus;
-import org.apache.http.client.ClientProtocolException;
-import org.apache.http.client.HttpClient;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.client.methods.HttpUriRequest;
-import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
-
import android.app.Activity;
import android.content.ContentValues;
-import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
-import android.net.ParseException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class JsonParseTestActivity extends Activity {
// Json data
- String jsonData = "[{\"id\":1, \"name\":class1, \"task_ids\":[1],\"teacher\":{\"id\":1, \"name\":teacher1}},"
+ /*String jsonData = "[{\"id\":1, \"name\":class1, \"task_ids\":[1],\"teacher\":{\"id\":1, \"name\":teacher1}},"
+ "{\"id\":2, \"name\":class2, \"task_ids\":[1,2],\"teacher\":{\"id\":2, \"name\":teacher2}},"
+ "{\"id\":3, \"name\":class3, \"task_ids\":[1,2,3],\"teacher\":{\"id\":3, \"name\":teacher3}},"
+ "{\"id\":4, \"name\":class4, \"task_ids\":[1,2,3,4],\"teacher\":{\"id\":4, \"name\":teacher4}},"
- + "{\"id\":5, \"name\":class5, \"task_ids\":[1,2,3,4,5],\"teacher\":{\"id\":5, \"name\":teacher5}}]";
+ + "{\"id\":5, \"name\":class5, \"task_ids\":[1,2,3,4,5],\"teacher\":{\"id\":5, \"name\":teacher5}}]";*/
ArrayList<Lecture> lectures, haveLecture, newLecture;
ArrayList<Teacher> teachers, haveTeacher, newTeacher;
TextView tv;
setLayout();
openTable();
getData();
- // String jsonData = httpGet("http://eagletmt.no-ip.org:4000/lectures");
- jsonParse(jsonData);
- classifyData();
- configListView();
+ DownloadTask task = new DownloadTask(this);
+ task.execute("http://131.112.197.37:3000/lectures");
+ //jsonParse(jsonData);
+ //classifyData();
+ //configListView();
}
void setLayout() {
null, null, null, "id");
}
- String httpGet(String uri) {
- HttpClient httpClient = new DefaultHttpClient();
- HttpUriRequest httpRequest = new HttpGet(uri);
- HttpResponse httpResponse = null;
- try {
- httpResponse = httpClient.execute(httpRequest);
- } catch (ClientProtocolException e) {
- } catch (IOException e) {
- }
- String json = null;
- if (httpResponse != null
- && httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
- HttpEntity httpEntity = httpResponse.getEntity();
- try {
- json = EntityUtils.toString(httpEntity);
- } catch (ParseException e) {
- } catch (IOException e) {
- } finally {
- try {
- httpEntity.consumeContent();
- } catch (IOException e) {
- }
- }
- }
- httpClient.getConnectionManager().shutdown();
- return json;
- }
-
void jsonParse(String data) {
try {
JSONArray root = new JSONArray(data);