Skip to content

Instantly share code, notes, and snippets.

@mangreen
Last active May 11, 2016 08:34
Show Gist options
  • Save mangreen/ad937ba226a0a77308dee0241c3679a7 to your computer and use it in GitHub Desktop.
Save mangreen/ad937ba226a0a77308dee0241c3679a7 to your computer and use it in GitHub Desktop.
JSONObject params = new JSONObject();
try
{
params.put("event", event);
params.put("partic", partic);
}
catch (Exception e)
{
e.printStackTrace();
}
DataManager.postData(DataManager.Msg_Event, this, UrlSetting.getUrl_Event(), DataManager.TYPE_POST, params, false, null);
public static synchronized void postData(final int msg_type,final DataManagerInterFace dataManagerInterFace,final String str_url,final String http_type, final JSONObject params,final boolean isAesEncode,final String sessionID)
{
new Thread()
{
@Override
public void run()
{
//開始取得資料
HttpResponseData httpResponseData = null; //http return str 沒資料時return ""
try
{
MyHttpUtility tmp = new MyHttpUtility(str_url,http_type, true,true,sessionID);
if(http_type.equals(TYPE_POST))
{
tmp.addParams(params,isAesEncode);
httpResponseData = tmp.doPost();
}
else if(http_type.equals(TYPE_GET))
{
httpResponseData = tmp.doGet();
}
}
catch(Exception e)
{
httpResponseData = new HttpResponseData(); //http return str 沒資料時return ""
}
finally
{
httpResponseData.m_DataManagerInterFace = dataManagerInterFace;
httpResponseData.m_DataMessage = msg_type;
Message msg = new Message();
msg.what = Msg_DataFinish;
msg.obj = httpResponseData;
m_DataManagerHandler.sendMessage(msg);
}
}
}.start();
}
public void addParams(JSONObject params,boolean isAesEncode)
{
if(params==null)
{
m_PostParameter = "";
return;
}
try
{
//是否加密
if(isAesEncode == true)
{
m_PostParameter = Util_Encrypt.encryptAES(AppSetting.getAESIV(), AppSetting.getAESKey(), params.toString());
}
else
{
m_PostParameter = params.toString();
}
}
catch (Exception e)
{
e.printStackTrace();
m_PostParameter = "";
}
finally
{
// MyLog.log("test", m_PostParameter);
}
}
public HttpResponseData doPost() //post data
{
DataOutputStream dos = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try
{
m_conn.connect();
try
{
dos = new DataOutputStream(m_conn.getOutputStream());
}
catch (Exception e)
{
m_conn.disconnect();
m_conn = (HttpURLConnection) m_url.openConnection();
setHttpType(m_HttpType);
m_conn.setDoOutput(true); //可送出資料
m_conn.setDoInput(true); //可取得資料
setConectionSetting();
setSession();
dos = new DataOutputStream(m_conn.getOutputStream());
}
dos.write(m_PostParameter.getBytes());
//file格式要求最後要write格式
//dos.writeBytes("\r\n");
//dos.writeBytes("--ahhjifeohiawf--\r\n");
dos.flush();
dos.close();
//read data
String encoding = m_conn.getContentEncoding();
is = m_conn.getInputStream();
int read = -1;
baos = new ByteArrayOutputStream();
while ((read = is.read()) != -1)
{
baos.write(read);
}
byte[] data = baos.toByteArray();
baos.close();
if (encoding != null)
{
m_HttpResponseData.m_Response = new String(data, encoding);
}
else
{
m_HttpResponseData.m_Response = new String(data);
}
//更新cookie
getSessionID();
}
catch (Exception e)
{
e.printStackTrace();
m_HttpResponseData.m_Response = "";
}
finally
{
try
{
dos.close();
is.close();
baos.close();
m_conn.disconnect();
}
catch (Exception e)
{
e.printStackTrace();
}
}
// MyLog.log("test", "url=>" + m_url);
// MyLog.log("test", "param=>" + m_PostParameter);
// MyLog.log("test", "response=>" + m_HttpResponseData.m_Response);
return m_HttpResponseData;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment