├── .gitignore
├── res
├── drawable-hdpi
│ └── icon.png
├── drawable-ldpi
│ └── icon.png
├── drawable-mdpi
│ └── icon.png
├── values
│ └── strings.xml
└── layout
│ └── main.xml
├── README
├── default.properties
├── AndroidManifest.xml
└── src
└── com
└── devstream
└── http
├── MainActivity.java
└── HttpClient.java
/.gitignore:
--------------------------------------------------------------------------------
1 | bin/
2 |
--------------------------------------------------------------------------------
/res/drawable-hdpi/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/stefanklumpp/Simple-HTTP-Client/HEAD/res/drawable-hdpi/icon.png
--------------------------------------------------------------------------------
/res/drawable-ldpi/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/stefanklumpp/Simple-HTTP-Client/HEAD/res/drawable-ldpi/icon.png
--------------------------------------------------------------------------------
/res/drawable-mdpi/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/stefanklumpp/Simple-HTTP-Client/HEAD/res/drawable-mdpi/icon.png
--------------------------------------------------------------------------------
/README:
--------------------------------------------------------------------------------
1 | This source code is related to an Android tutorial, which is published here: http://devstream.stefanklumpp.com/2009/11/android-simple-httpclient-to.html
2 |
--------------------------------------------------------------------------------
/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | Hello World, HttpClient!
4 | Simple HTTP Client
5 |
6 |
--------------------------------------------------------------------------------
/res/layout/main.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
12 |
13 |
--------------------------------------------------------------------------------
/default.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system use,
7 | # "build.properties", and override values to adapt the script to your
8 | # project structure.
9 |
10 | # Indicates whether an apk should be generated for each density.
11 | split.density=false
12 | # Project target.
13 | target=android-4
14 |
--------------------------------------------------------------------------------
/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/src/com/devstream/http/MainActivity.java:
--------------------------------------------------------------------------------
1 | /***
2 | Copyright (c) 2009
3 | Author: Stefan Klumpp
4 | Web: http://stefanklumpp.com
5 |
6 | Licensed under the Apache License, Version 2.0 (the "License"); you may
7 | not use this file except in compliance with the License. You may obtain
8 | a copy of the License at
9 | http://www.apache.org/licenses/LICENSE-2.0
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 |
17 |
18 | package com.devstream.http;
19 |
20 | import org.json.JSONException;
21 | import org.json.JSONObject;
22 | import android.app.Activity;
23 | import android.os.Bundle;
24 | import android.util.Log;
25 |
26 | public class MainActivity extends Activity {
27 | private static final String TAG = "MainActivity";
28 | private static final String URL = "http://www.yourdomain.com:80";
29 |
30 | @Override
31 | public void onCreate(Bundle savedInstanceState) {
32 | super.onCreate(savedInstanceState);
33 | setContentView(R.layout.main);
34 |
35 | // JSON object to hold the information, which is sent to the server
36 | JSONObject jsonObjSend = new JSONObject();
37 |
38 | try {
39 | // Add key/value pairs
40 | jsonObjSend.put("key_1", "value_1");
41 | jsonObjSend.put("key_2", "value_2");
42 |
43 | // Add a nested JSONObject (e.g. for header information)
44 | JSONObject header = new JSONObject();
45 | header.put("deviceType","Android"); // Device type
46 | header.put("deviceVersion","2.0"); // Device OS version
47 | header.put("language", "es-es"); // Language of the Android client
48 | jsonObjSend.put("header", header);
49 |
50 | // Output the JSON object we're sending to Logcat:
51 | Log.i(TAG, jsonObjSend.toString(2));
52 |
53 | } catch (JSONException e) {
54 | e.printStackTrace();
55 | }
56 |
57 | // Send the HttpPostRequest and receive a JSONObject in return
58 | JSONObject jsonObjRecv = HttpClient.SendHttpPost(URL, jsonObjSend);
59 |
60 | /*
61 | * From here on do whatever you want with your JSONObject, e.g.
62 | * 1) Get the value for a key: jsonObjRecv.get("key");
63 | * 2) Get a nested JSONObject: jsonObjRecv.getJSONObject("key")
64 | * 3) Get a nested JSONArray: jsonObjRecv.getJSONArray("key")
65 | */
66 |
67 |
68 | }
69 | }
--------------------------------------------------------------------------------
/src/com/devstream/http/HttpClient.java:
--------------------------------------------------------------------------------
1 | /***
2 | Copyright (c) 2009
3 | Author: Stefan Klumpp
4 | Web: http://stefanklumpp.com
5 |
6 | Licensed under the Apache License, Version 2.0 (the "License"); you may
7 | not use this file except in compliance with the License. You may obtain
8 | a copy of the License at
9 | http://www.apache.org/licenses/LICENSE-2.0
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 |
17 | package com.devstream.http;
18 |
19 | import java.io.BufferedReader;
20 | import java.io.IOException;
21 | import java.io.InputStream;
22 | import java.io.InputStreamReader;
23 | import java.util.zip.GZIPInputStream;
24 | import org.apache.http.Header;
25 | import org.apache.http.HttpEntity;
26 | import org.apache.http.HttpResponse;
27 | import org.apache.http.client.methods.HttpPost;
28 | import org.apache.http.entity.StringEntity;
29 | import org.apache.http.impl.client.DefaultHttpClient;
30 | import org.json.JSONObject;
31 | import android.util.Log;
32 |
33 | public class HttpClient {
34 | private static final String TAG = "HttpClient";
35 |
36 | public static JSONObject SendHttpPost(String URL, JSONObject jsonObjSend) {
37 |
38 | try {
39 | DefaultHttpClient httpclient = new DefaultHttpClient();
40 | HttpPost httpPostRequest = new HttpPost(URL);
41 |
42 | StringEntity se;
43 | se = new StringEntity(jsonObjSend.toString());
44 |
45 | // Set HTTP parameters
46 | httpPostRequest.setEntity(se);
47 | httpPostRequest.setHeader("Accept", "application/json");
48 | httpPostRequest.setHeader("Content-type", "application/json");
49 | httpPostRequest.setHeader("Accept-Encoding", "gzip"); // only set this parameter if you would like to use gzip compression
50 |
51 | long t = System.currentTimeMillis();
52 | HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
53 | Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis()-t) + "ms]");
54 |
55 | // Get hold of the response entity (-> the data):
56 | HttpEntity entity = response.getEntity();
57 |
58 | if (entity != null) {
59 | // Read the content stream
60 | InputStream instream = entity.getContent();
61 | Header contentEncoding = response.getFirstHeader("Content-Encoding");
62 | if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
63 | instream = new GZIPInputStream(instream);
64 | }
65 |
66 | // convert content stream to a String
67 | String resultString= convertStreamToString(instream);
68 | instream.close();
69 | resultString = resultString.substring(1,resultString.length()-1); // remove wrapping "[" and "]"
70 |
71 | // Transform the String into a JSONObject
72 | JSONObject jsonObjRecv = new JSONObject(resultString);
73 | // Raw DEBUG output of our received JSON object:
74 | Log.i(TAG,"\n"+jsonObjRecv.toString()+"\n");
75 |
76 | return jsonObjRecv;
77 | }
78 |
79 | }
80 | catch (Exception e)
81 | {
82 | // More about HTTP exception handling in another tutorial.
83 | // For now we just print the stack trace.
84 | e.printStackTrace();
85 | }
86 | return null;
87 | }
88 |
89 |
90 | private static String convertStreamToString(InputStream is) {
91 | /*
92 | * To convert the InputStream to String we use the BufferedReader.readLine()
93 | * method. We iterate until the BufferedReader return null which means
94 | * there's no more data to read. Each line will appended to a StringBuilder
95 | * and returned as String.
96 | *
97 | * (c) public domain: http://senior.ceng.metu.edu.tr/2009/praeda/2009/01/11/a-simple-restful-client-at-android/
98 | */
99 | BufferedReader reader = new BufferedReader(new InputStreamReader(is));
100 | StringBuilder sb = new StringBuilder();
101 |
102 | String line = null;
103 | try {
104 | while ((line = reader.readLine()) != null) {
105 | sb.append(line + "\n");
106 | }
107 | } catch (IOException e) {
108 | e.printStackTrace();
109 | } finally {
110 | try {
111 | is.close();
112 | } catch (IOException e) {
113 | e.printStackTrace();
114 | }
115 | }
116 | return sb.toString();
117 | }
118 |
119 | }
120 |
--------------------------------------------------------------------------------