こんにちはまず、別のクラスHttpUtil.javaを作成する必要があります。次のコードを参照してください。
public class HttpUtil {
// lat=50.2911 lon=8.9842
private final static String TAG = "DealApplication:HttpUtil";
public static String get(String url) throws ClientProtocolException,
IOException {
Log.d(TAG, "HTTP POST " + url);
HttpGet post = new HttpGet(url);
HttpResponse response = executeMethod(post);
return getResponseAsString(response);
}
public static String post(String url, HashMap<String, String> httpParameters)
throws ClientProtocolException, IOException {
Log.d(TAG, "HTTP POST " + url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
httpParameters.size());
Set<String> httpParameterKeys = httpParameters.keySet();
for (String httpParameterKey : httpParameterKeys) {
nameValuePairs.add(new BasicNameValuePair(httpParameterKey,
httpParameters.get(httpParameterKey)));
}
HttpPost method = new HttpPost(url);
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairs);
System.out.println("**************Request=>"+urlEncodedFormEntity.toString());
method.setEntity(urlEncodedFormEntity);
HttpResponse response = executeMethod(method);
return getResponseAsString(response);
}
private static HttpResponse executeMethod(HttpRequestBase method)
throws ClientProtocolException, IOException {
HttpResponse response = null;
HttpClient client = new DefaultHttpClient();
response = client.execute(method);
Log.d(TAG, "executeMethod=" + response.getStatusLine());
return response;
}
private static String getResponseAsString(HttpResponse response)
throws IllegalStateException, IOException {
String content = null;
InputStream stream = null;
try {
if (response != null) {
stream = response.getEntity().getContent();
InputStreamReader reader = new InputStreamReader(stream);
BufferedReader buffer = new BufferedReader(reader);
StringBuilder sb = new StringBuilder();
String cur;
while ((cur = buffer.readLine()) != null) {
sb.append(cur + "\n");
}
content = sb.toString();
System.out.println("**************Response =>"+content);
}
} finally {
if (stream != null) {
stream.close();
}
}
return content;
}
}