use of org.qii.weiciyuan.support.error.WeiboException in project weiciyuan by qii.
the class JavaHttpUtility method doUploadFile.
public boolean doUploadFile(String urlStr, Map<String, String> param, String path, String imageParamName, final FileUploaderHttpHelper.ProgressListener listener) throws WeiboException {
String BOUNDARYSTR = getBoundry();
File targetFile = new File(path);
byte[] barry = null;
int contentLength = 0;
String sendStr = "";
try {
barry = ("--" + BOUNDARYSTR + "--\r\n").getBytes("UTF-8");
sendStr = getBoundaryMessage(BOUNDARYSTR, param, imageParamName, new File(path).getName(), "image/png");
contentLength = sendStr.getBytes("UTF-8").length + (int) targetFile.length() + 2 * barry.length;
} catch (UnsupportedEncodingException e) {
}
int totalSent = 0;
String lenstr = Integer.toString(contentLength);
HttpURLConnection urlConnection = null;
BufferedOutputStream out = null;
FileInputStream fis = null;
GlobalContext globalContext = GlobalContext.getInstance();
String errorStr = globalContext.getString(R.string.timeout);
globalContext = null;
try {
URL url = null;
url = new URL(urlStr);
Proxy proxy = getProxy();
if (proxy != null) {
urlConnection = (HttpURLConnection) url.openConnection(proxy);
} else {
urlConnection = (HttpURLConnection) url.openConnection();
}
urlConnection.setConnectTimeout(UPLOAD_CONNECT_TIMEOUT);
urlConnection.setReadTimeout(UPLOAD_READ_TIMEOUT);
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Connection", "Keep-Alive");
urlConnection.setRequestProperty("Charset", "UTF-8");
urlConnection.setRequestProperty("Content-type", "multipart/form-data;boundary=" + BOUNDARYSTR);
urlConnection.setRequestProperty("Content-Length", lenstr);
((HttpURLConnection) urlConnection).setFixedLengthStreamingMode(contentLength);
urlConnection.connect();
out = new BufferedOutputStream(urlConnection.getOutputStream());
out.write(sendStr.getBytes("UTF-8"));
totalSent += sendStr.getBytes("UTF-8").length;
fis = new FileInputStream(targetFile);
int bytesRead;
int bytesAvailable;
int bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024;
bytesAvailable = fis.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fis.read(buffer, 0, bufferSize);
long transferred = 0;
final Thread thread = Thread.currentThread();
while (bytesRead > 0) {
if (thread.isInterrupted()) {
targetFile.delete();
throw new InterruptedIOException();
}
out.write(buffer, 0, bufferSize);
bytesAvailable = fis.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fis.read(buffer, 0, bufferSize);
transferred += bytesRead;
if (transferred % 50 == 0) {
out.flush();
}
if (listener != null) {
listener.transferred(transferred);
}
}
out.write(barry);
totalSent += barry.length;
out.write(barry);
totalSent += barry.length;
out.flush();
out.close();
if (listener != null) {
listener.waitServerResponse();
}
int status = urlConnection.getResponseCode();
if (status != HttpURLConnection.HTTP_OK) {
String error = handleError(urlConnection);
throw new WeiboException(error);
}
targetFile.delete();
} catch (IOException e) {
e.printStackTrace();
throw new WeiboException(errorStr, e);
} finally {
Utility.closeSilently(fis);
Utility.closeSilently(out);
if (urlConnection != null) {
urlConnection.disconnect();
}
}
return true;
}
use of org.qii.weiciyuan.support.error.WeiboException in project weiciyuan by qii.
the class JavaHttpUtility method readResult.
private String readResult(HttpURLConnection urlConnection) throws WeiboException {
InputStream is = null;
BufferedReader buffer = null;
GlobalContext globalContext = GlobalContext.getInstance();
String errorStr = globalContext.getString(R.string.timeout);
globalContext = null;
try {
is = urlConnection.getInputStream();
String content_encode = urlConnection.getContentEncoding();
if (!TextUtils.isEmpty(content_encode) && content_encode.equals("gzip")) {
is = new GZIPInputStream(is);
}
buffer = new BufferedReader(new InputStreamReader(is));
StringBuilder strBuilder = new StringBuilder();
String line;
while ((line = buffer.readLine()) != null) {
strBuilder.append(line);
}
// AppLogger.d("result=" + strBuilder.toString());
return strBuilder.toString();
} catch (IOException e) {
e.printStackTrace();
throw new WeiboException(errorStr, e);
} finally {
Utility.closeSilently(is);
Utility.closeSilently(buffer);
urlConnection.disconnect();
}
}
use of org.qii.weiciyuan.support.error.WeiboException in project weiciyuan by qii.
the class JavaHttpUtility method doGet.
public String doGet(String urlStr, Map<String, String> param) throws WeiboException {
GlobalContext globalContext = GlobalContext.getInstance();
String errorStr = globalContext.getString(R.string.timeout);
globalContext = null;
InputStream is = null;
try {
StringBuilder urlBuilder = new StringBuilder(urlStr);
urlBuilder.append("?").append(Utility.encodeUrl(param));
URL url = new URL(urlBuilder.toString());
AppLogger.d("get request" + url);
Proxy proxy = getProxy();
HttpURLConnection urlConnection;
if (proxy != null) {
urlConnection = (HttpURLConnection) url.openConnection(proxy);
} else {
urlConnection = (HttpURLConnection) url.openConnection();
}
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(false);
urlConnection.setConnectTimeout(CONNECT_TIMEOUT);
urlConnection.setReadTimeout(READ_TIMEOUT);
urlConnection.setRequestProperty("Connection", "Keep-Alive");
urlConnection.setRequestProperty("Charset", "UTF-8");
urlConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
urlConnection.connect();
return handleResponse(urlConnection);
} catch (IOException e) {
e.printStackTrace();
throw new WeiboException(errorStr, e);
}
}
use of org.qii.weiciyuan.support.error.WeiboException in project weiciyuan by qii.
the class JavaHttpUtility method handleError.
private String handleError(HttpURLConnection urlConnection) throws WeiboException {
String result = readError(urlConnection);
String err = null;
int errCode = 0;
try {
AppLogger.e("error=" + result);
JSONObject json = new JSONObject(result);
err = json.optString("error_description", "");
if (TextUtils.isEmpty(err)) {
err = json.getString("error");
}
errCode = json.getInt("error_code");
WeiboException exception = new WeiboException();
exception.setError_code(errCode);
exception.setOriError(err);
if (errCode == ErrorCode.EXPIRED_TOKEN || errCode == ErrorCode.INVALID_TOKEN) {
Utility.showExpiredTokenDialogOrNotification();
}
throw exception;
} catch (JSONException e) {
e.printStackTrace();
}
return result;
}
use of org.qii.weiciyuan.support.error.WeiboException in project weiciyuan by qii.
the class FetchNewMsgService method onHandleIntent.
@Override
protected void onHandleIntent(Intent intent) {
if (intent == null) {
return;
}
String action = intent.getAction();
if (ACTION_ALARM_MANAGER.equals(action)) {
AppLogger.i("FetchNewMsgService is started by " + ACTION_ALARM_MANAGER);
if (SettingUtility.disableFetchAtNight() && isNowNight()) {
AppLogger.i("FetchNewMsgService is disabled at night, so give up");
return;
}
} else if (ACTION_OPEN_APP.equals(action)) {
//empty
AppLogger.i("FetchNewMsgService is started by " + ACTION_OPEN_APP);
} else {
AppLogger.i("FetchNewMsgService receive Intent whose Action is empty");
//why System send Intent object whose Action is empty? fuck google, it is impossible according to api documents when this service flag is START_NOT_STICKY
return;
}
List<AccountBean> accountBeanList = AccountDBTask.getAccountList();
if (accountBeanList.size() == 0) {
return;
}
for (AccountBean account : accountBeanList) {
try {
AppLogger.i("FetchNewMsgService start fetch " + account.getUsernick() + "'s unread data");
fetchMsg(account);
} catch (WeiboException e) {
e.printStackTrace();
}
}
AppLogger.i("FetchNewMsgService finished");
}
Aggregations