public String getData(String url, Map<String, String> headers, Map<String, String>[] params, boolean isPostType) throws Exception {
StringBuilder sb = new StringBuilder();
InputStream is = null;
DefaultHttpClient httpclient = null;
BufferedReader reader = null;
try {
httpclient = new DefaultHttpClient();
HttpParams httpParams = httpclient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, HTTP_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParams, HTTP_TIMEOUT);
HttpResponse response = null;
if (params != null && isPostType) { // Post 방식 일때
HttpPost httpPost = new HttpPost(url);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
for (int i=0;i<params.length;i++) {
for (Map.Entry<String, String> entry: params[i].entrySet()) {
nameValuePairs.add(new BasicNameValuePair(entry.getKey(), (String)entry.getValue()));
Util.log("param :: "+ entry.getKey() +"="+entry.getValue());
}
}
UrlEncodedFormEntity entityRequest = new UrlEncodedFormEntity(nameValuePairs, "UTF-8");
httpPost.setEntity(entityRequest);
if (headers != null) {
for (Map.Entry<String, String> entry2: headers.entrySet()) {
httpPost.addHeader(entry2.getKey(), entry2.getValue());
Util.log("header :: "+entry2.getKey()+"="+ entry2.getValue());
}
}
response = httpclient.execute(httpPost);
//DefaultHttpClient 객체의 execute 함수의 파라메터에 httpRequest 객체 (httpPost, httpGet)를 넘겨주면 HttpResponse 객체를 리턴
} else { //Get 방식일때
if (params != null) {
if (params[0].entrySet().size() > 0) url += "?";
for (Map.Entry<String, ?> entry : params[0].entrySet()) {
if (entry.getValue() instanceof String[]) {
String[] values = (String[]) entry.getValue();
for (int i = 0; i < values.length; i++) {
url += entry.getKey() + "=" + values[i] + "&";
}
} else {
url += entry.getKey() + "=" + entry.getValue() + "&";
}
}
}
HttpGet httpget = new HttpGet(url);
Util.log("network", "url = "+url);
if (headers != null) {
for (Map.Entry<String, String> entry2: headers.entrySet()) {
httpget.addHeader(entry2.getKey(), entry2.getValue());
}
}
response = httpclient.execute(httpget);
}
is = response.getEntity().getContent();
reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
} catch (Exception e) {
Util.logError(e);
throw e;
} finally {
try {
if (is != null) is.close();
if (reader != null) reader.close();
} catch (Exception e) {throw e;}
}
xmlValue = sb.toString();
Log.d("debug", "url = " + url);
Log.d("debug", xmlValue);
return xmlValue;
}
댓글 없음:
댓글 쓰기