Skip to content

Instantly share code, notes, and snippets.

@ccapndave
Created June 10, 2012 15:37
Show Gist options
  • Select an option

  • Save ccapndave/2906266 to your computer and use it in GitHub Desktop.

Select an option

Save ccapndave/2906266 to your computer and use it in GitHub Desktop.
private String doSync(String operation, IClient client, RequestFunction function, AMFDataList params, String host, String data) {
StringBuffer resultBuffer = new StringBuffer();
HttpClient httpClient = null;
BufferedReader in = null;
OutputStreamWriter out = null;
try {
httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_0); // disable chunking
List<NameValuePair> getParams = new ArrayList<NameValuePair>();
getParams.add(new BasicNameValuePair("operation",operation));
getParams.add(new BasicNameValuePair("params", params.getString(PARAM1)));
HttpPost method = new HttpPost(URIUtils.createURI("http", host.substring(7, host.length()), -1, "/sync_controller.php", URLEncodedUtils.format(getParams, "UTF-8"), null));
if (data != null) {
byte[] dataBytes = data.getBytes("UTF-8");
//MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
UploadProgressListener uploadProgressListener = new UploadProgressListener(client, dataBytes.length, getLogger());
MultipartEntity reqEntity = new CountingMultiPartEntity(uploadProgressListener);
reqEntity.addPart("datafile", new InputStreamKnownSizeBody(new ByteArrayInputStream(dataBytes), dataBytes.length, "application/octet-stream", "vulcandatafile"));
method.setEntity(reqEntity);
}
if (data != null) client.call("syncSittingMessage", null, "<message><![CDATA[<b>Transferring sitting data... </b> ]]></message>");
HttpResponse httpResponse = httpClient.execute(method);
if (data != null) client.call("syncSittingMessage", null, "<message><![CDATA[<br/><font color='#4545A0'>Data transfer complete.</font><br/><br/>]]></message>");
in = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine); // test
String type = "";
if (inputLine.startsWith("<message>") && inputLine.endsWith("</message>")) {
type = "message";
} else if (inputLine.startsWith("<error>") && inputLine.endsWith("</error>")) {
type = "error";
}
if (type.equals("message") || type.equals("error")) {
client.call("syncSittingMessage", null, inputLine);
}
if (!type.equals("message"))
resultBuffer.append(inputLine);
}
System.out.println("GOT TO THE END!");
} catch (IOException e) {
client.call("syncSittingMessage", null, "<error><![CDATA[" + e.getMessage() + "]]><br/><br/></error>");
e.printStackTrace();
} catch (URISyntaxException e) {
client.call("syncSittingMessage", null, "<error><![CDATA[" + e.getMessage() + "]]><br/><br/></error>");
e.printStackTrace();
} finally {
try {
if (in != null) in.close();
if (out != null) out.close();
httpClient.getConnectionManager().shutdown();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultBuffer.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment