99 lines
3.0 KiB
Java
99 lines
3.0 KiB
Java
package org.pticlic.model;
|
|
|
|
/***
|
|
Copyright (c) 2009
|
|
Author: Stefan Klumpp <stefan.klumpp@gmail.com>
|
|
Web: http://stefanklumpp.com
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
not use this file except in compliance with the License. You may obtain
|
|
a copy of the License at
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
import java.util.zip.GZIPInputStream;
|
|
|
|
import org.apache.http.Header;
|
|
import org.apache.http.HttpEntity;
|
|
import org.apache.http.HttpResponse;
|
|
import org.apache.http.client.methods.HttpGet;
|
|
import org.apache.http.impl.client.DefaultHttpClient;
|
|
|
|
import android.util.Log;
|
|
|
|
public class HttpClient {
|
|
private static final String TAG = "HttpClient";
|
|
|
|
public static String SendHttpPost(String URL) {
|
|
|
|
try {
|
|
DefaultHttpClient httpclient = new DefaultHttpClient();
|
|
HttpGet httpPostRequest = new HttpGet(URL);
|
|
|
|
//StringEntity se;
|
|
//se = new StringEntity(str);
|
|
|
|
// Set HTTP parameters
|
|
//httpPostRequest.setEntity(se);
|
|
httpPostRequest.setHeader("Accept", "application/json");
|
|
httpPostRequest.setHeader("Content-type", "application/json");
|
|
httpPostRequest.setHeader("Accept-Encoding", "gzip"); // only set this parameter if you would like to use gzip compression
|
|
|
|
long t = System.currentTimeMillis();
|
|
HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
|
|
Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis()-t) + "ms]");
|
|
|
|
// Get hold of the response entity (-> the data):
|
|
HttpEntity entity = response.getEntity();
|
|
|
|
if (entity != null) {
|
|
// Read the content stream
|
|
InputStream instream = entity.getContent();
|
|
Header contentEncoding = response.getFirstHeader("Content-Encoding");
|
|
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
|
|
instream = new GZIPInputStream(instream);
|
|
}
|
|
|
|
// convert content stream to a String
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
String line = null;
|
|
try {
|
|
while ((line = reader.readLine()) != null) {
|
|
sb.append(line + "\n");
|
|
}
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
} finally {
|
|
try {
|
|
instream.close();
|
|
instream = null;
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
return sb.toString();
|
|
} else {
|
|
return null;
|
|
}
|
|
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
// More about HTTP exception handling in another tutorial.
|
|
// For now we just print the stack trace.
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
} |