diff --git a/android-http-get-json/AndroidManifest.xml b/android-http-get-json/AndroidManifest.xml new file mode 100644 index 0000000..edcc891 --- /dev/null +++ b/android-http-get-json/AndroidManifest.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + diff --git a/android-http-get-json/ic_launcher-web.png b/android-http-get-json/ic_launcher-web.png new file mode 100644 index 0000000..a18cbb4 Binary files /dev/null and b/android-http-get-json/ic_launcher-web.png differ diff --git a/android-http-get-json/res/drawable-hdpi/ic_launcher.png b/android-http-get-json/res/drawable-hdpi/ic_launcher.png new file mode 100644 index 0000000..288b665 Binary files /dev/null and b/android-http-get-json/res/drawable-hdpi/ic_launcher.png differ diff --git a/android-http-get-json/res/layout/main.xml b/android-http-get-json/res/layout/main.xml new file mode 100644 index 0000000..5888a6b --- /dev/null +++ b/android-http-get-json/res/layout/main.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + diff --git a/android-http-get-json/res/menu/main.xml b/android-http-get-json/res/menu/main.xml new file mode 100644 index 0000000..c002028 --- /dev/null +++ b/android-http-get-json/res/menu/main.xml @@ -0,0 +1,9 @@ + + + + + diff --git a/android-http-get-json/res/values/dimens.xml b/android-http-get-json/res/values/dimens.xml new file mode 100644 index 0000000..55c1e59 --- /dev/null +++ b/android-http-get-json/res/values/dimens.xml @@ -0,0 +1,7 @@ + + + + 16dp + 16dp + + diff --git a/android-http-get-json/res/values/strings.xml b/android-http-get-json/res/values/strings.xml new file mode 100644 index 0000000..f21c0a9 --- /dev/null +++ b/android-http-get-json/res/values/strings.xml @@ -0,0 +1,8 @@ + + + + HTTP GET JSON APP + Settings + Hello world! + + diff --git a/android-http-get-json/res/values/styles.xml b/android-http-get-json/res/values/styles.xml new file mode 100644 index 0000000..6ce89c7 --- /dev/null +++ b/android-http-get-json/res/values/styles.xml @@ -0,0 +1,20 @@ + + + + + + + + + diff --git a/android-http-get-json/src/com/hmkcode/android/MainActivity.java b/android-http-get-json/src/com/hmkcode/android/MainActivity.java new file mode 100644 index 0000000..db82e05 --- /dev/null +++ b/android-http-get-json/src/com/hmkcode/android/MainActivity.java @@ -0,0 +1,132 @@ +package com.hmkcode.android; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.Iterator; + +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 android.net.ConnectivityManager; +import android.net.NetworkInfo; +import android.os.AsyncTask; +import android.os.Bundle; +import android.util.Log; +import android.widget.EditText; +import android.widget.TextView; +import android.widget.Toast; +import android.app.Activity; + +public class MainActivity extends Activity { + + EditText etResponse; + TextView tvIsConnected; + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.main); + + // get reference to the views + etResponse = (EditText) findViewById(R.id.etResponse); + tvIsConnected = (TextView) findViewById(R.id.tvIsConnected); + + // check if you are connected or not + if(isConnected()){ + tvIsConnected.setBackgroundColor(0xFF00CC00); + tvIsConnected.setText("You are conncted"); + } + else{ + tvIsConnected.setText("You are NOT conncted"); + } + + + // 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; + + } + + public boolean isConnected(){ + ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE); + NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); + if (networkInfo != null && networkInfo.isConnected()) + return true; + else + return false; + } + private class HttpAsyncTask extends AsyncTask { + @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(); + try { + JSONObject json = new JSONObject(result); + + String str = ""; + + JSONArray articles = json.getJSONArray("articleList"); + str += "articles length = "+json.getJSONArray("articleList").length(); + str += "\n--------\n"; + str += "names: "+articles.getJSONObject(0).names(); + str += "\n--------\n"; + str += "url: "+articles.getJSONObject(0).getString("url"); + + etResponse.setText(str); + //etResponse.setText(json.toString(1)); + + } catch (JSONException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } +}