import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;
import com.lge.readersworld.util.Define;
import com.lge.readersworld.util.RWDeviceUtil;
import com.lge.readersworld.util.RWPreference;
import com.lge.readersworld.util.Util;
public class RWServerNetwork {
public static final int HTTP_TIMEOUT = 5000;
private String xmlValue = null;
private String fileLength = null;
private String fileName = null;
public static HashMap<String, String> getHeader(Context context) {
Display defaultDisplay = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = defaultDisplay.getWidth();
int height = defaultDisplay.getHeight();
HashMap<String, String> header = new HashMap<String, String>();
header.put("x-lg-accept", "text/xml");
// header.put("x-lg-language", "EN");
// header.put("x-lg-country", "AU");
// header.put("x-lg-model", "GW820");
// jh.lee : 선택한 국가에 맞춰서 Header를 보낸다.
header.put("x-lg-language", RWPreference.getInstance(context).getData(Define.SHARED_LOGIN_LANGUAGECODE, "EN"));
header.put("x-lg-country", RWPreference.getInstance(context).getData(Define.SHARED_LOGIN_COUNTRYCODE, "US"));
header.put("x-lg-model", RWDeviceUtil.getModelInfo());
//-------------------------------------------------------------------------------------------------------------------
header.put("x-lg-agent", "LGRW;1.0.0.1;id");
header.put("x-lg-platform", "AN"+Build.VERSION.RELEASE);
if (RWDeviceUtil.isLandscape(context)) {
header.put("x-lg-resolution", width + "X" + height);
} else {
header.put("x-lg-resolution", height + "X" + width);
}
return header;
}
/**
* Carrier 코드를 출력한다.
*
* @param context
* @return
*/
public static String getCRCode(Context context) {
return "NET_2015";
}
/**
* XML에서 원하는 값을 추출한다. Level1만 지원함.
* @param str
* @return
*/
public String parser(String str) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputStream istream = new ByteArrayInputStream(xmlValue.getBytes("utf-8"));
Document doc = builder.parse(istream);
Element order = doc.getDocumentElement();
NodeList items = order.getElementsByTagName(str);
Node item = items.item(0);
if (item==null) return null;
Node text = item.getFirstChild();
String ItemName = text.getNodeValue();
return ItemName;
} catch (Exception e) {
Util.logError(e);
}
return null;
}
public Element getParser() {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputStream istream = new ByteArrayInputStream(xmlValue.getBytes("utf-8"));
Document doc = builder.parse(istream);
Element order = doc.getDocumentElement();
return order;
} catch (Exception e) {
Util.logError(e);
}
return null;
}
public static Element getParser(String filename) {
try {
String xmlValue = Util.getFileContent(filename);
if (xmlValue==null) return null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputStream istream = new ByteArrayInputStream(xmlValue.getBytes("utf-8"));
Document doc = builder.parse(istream);
Element order = doc.getDocumentElement();
return order;
} catch (Exception e) {
Util.logError(e);
}
return null;
}
@SuppressWarnings("unchecked")
public String getData(String url, Map<String, String> headers, Map<String, String> params, boolean isPostType) throws Exception {
return getData(url, headers, new Map[]{params}, isPostType);
}
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) {
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);
} else {
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;
}
public InputStream getDownload(String url, Map<String, String> headers, Map<String, String> params) throws Exception {
InputStream is = null;
DefaultHttpClient httpclient = null;
try {
if (params != null) {
url +="?";
for (Map.Entry<String, String> entry: params.entrySet()) {
url += entry.getKey()+"="+URLEncoder.encode(entry.getValue())+"&";
}
}
Util.log("network", "url = "+url);
HttpGet httppost = new HttpGet(url);
if (headers != null) {
for (Map.Entry<String, String> entry: headers.entrySet()) {
httppost.addHeader(entry.getKey(), entry.getValue());
}
}
httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
Header[] rheaders = response.getAllHeaders();
for (int i=0;i<rheaders.length;i++) {
if (rheaders[i].getName().toLowerCase().equals("result")) {
boolean tmp = "700".equals(rheaders[i].getValue());
if (tmp==false) throw new Exception("Server Error.");
}
if (rheaders[i].getName().toLowerCase().equals("content-length")) {
fileLength = rheaders[i].getValue();
}
if (rheaders[i].getName().toLowerCase().equals("content-disposition")) {
String tmp = rheaders[i].getValue();
fileName = tmp.substring(tmp.indexOf("filename=")+9);
}
Util.log("debug", "headers["+i+"] "+ rheaders[i].getName()+"="+rheaders[i].getValue());
}
is = response.getEntity().getContent();
return is;
} catch (Exception e) {
Util.logError(e);
throw e;
}
}
public int getFileLength() {
int size = 0;
try {
size = Integer.parseInt(fileLength);
}catch (Exception e) {}
return size;
}
public String getFileName() {
return fileName;
}
}
댓글 없음:
댓글 쓰기