OSDN Git Service

connect server and get json
authorYuta Kawabe <yuyu3165@gmail>
Wed, 27 Jun 2012 06:34:32 +0000 (15:34 +0900)
committerYuta Kawabe <yuyu3165@gmail>
Wed, 27 Jun 2012 06:34:32 +0000 (15:34 +0900)
Downloadown/AndroidManifest.xml
Downloadown/src/jp/ac/titech/sharp4k/cuten/DownloadTask.java [new file with mode: 0644]
Downloadown/src/jp/ac/titech/sharp4k/cuten/JsonParseTestActivity.java

index d821607..42a8bfc 100644 (file)
@@ -3,6 +3,8 @@
     package="jp.ac.titech.sharp4k.cuten"
     android:versionCode="1"
     android:versionName="1.0" >
+    
+    <uses-permission android:name="android.permission.INTERNET" />
 
     <uses-sdk android:minSdkVersion="13" />
 
diff --git a/Downloadown/src/jp/ac/titech/sharp4k/cuten/DownloadTask.java b/Downloadown/src/jp/ac/titech/sharp4k/cuten/DownloadTask.java
new file mode 100644 (file)
index 0000000..b78e46d
--- /dev/null
@@ -0,0 +1,68 @@
+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();
+       }
+
+}
index f8afe9a..23cc26f 100644 (file)
@@ -1,29 +1,14 @@
 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;
@@ -34,11 +19,11 @@ import android.widget.TextView;
 
 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;
@@ -59,10 +44,11 @@ public class JsonParseTestActivity extends Activity {
                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() {
@@ -114,34 +100,6 @@ public class JsonParseTestActivity extends Activity {
                                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);