use of com.ichi2.anki.exception.UnknownHttpResponseException in project AnkiChinaAndroid by ankichinateam.
the class MediaSyncer method _downloadFiles.
private void _downloadFiles(List<String> fnames) {
mCol.log(fnames.size() + " files to fetch");
while (fnames.size() > 0) {
try {
List<String> top = fnames.subList(0, Math.min(fnames.size(), Consts.SYNC_ZIP_COUNT));
mCol.log("fetch " + top);
ZipFile zipData = mServer.downloadFiles(top);
int cnt = mCol.getMedia().addFilesFromZip(zipData);
mDownloadCount += cnt;
mCol.log("received " + cnt + " files");
// if we've reached the end and clear the fnames list manually.
if (cnt == fnames.size()) {
fnames.clear();
} else {
fnames = fnames.subList(cnt, fnames.size());
}
mCon.publishProgress(String.format(AnkiDroidApp.getAppResources().getString(R.string.sync_media_downloaded_count), mDownloadCount));
} catch (IOException | UnknownHttpResponseException e) {
Timber.e(e, "Error downloading media files");
throw new RuntimeException(e);
}
}
}
use of com.ichi2.anki.exception.UnknownHttpResponseException in project AnkiChinaAndroid by ankichinateam.
the class RemoteMediaServer method mediaChanges.
// args: lastUsn
public JSONArray mediaChanges(int lastUsn) throws UnknownHttpResponseException, MediaSyncException {
try {
mPostVars = new HashMap<>();
mPostVars.put("sk", mSKey);
Response resp = super.req("mediaChanges", HttpSyncer.getInputStream(Utils.jsonToString(new JSONObject().put("lastUsn", lastUsn))));
JSONObject jresp = new JSONObject(resp.body().string());
return _dataOnly(jresp, JSONArray.class);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of com.ichi2.anki.exception.UnknownHttpResponseException in project AnkiChinaAndroid by ankichinateam.
the class RemoteMediaServer method downloadFiles.
/**
* args: files
* <br>
* This method returns a ZipFile with the OPEN_DELETE flag, ensuring that the file on disk will
* be automatically deleted when the stream is closed.
*/
public ZipFile downloadFiles(List<String> top) throws UnknownHttpResponseException {
Response resp = null;
try {
resp = super.req("downloadFiles", HttpSyncer.getInputStream(Utils.jsonToString(new JSONObject().put("files", new JSONArray(top)))));
String zipPath = mCol.getPath().replaceFirst("collection\\.anki2$", "tmpSyncFromServer.zip");
// retrieve contents and save to file on disk:
super.writeToFile(resp.body().byteStream(), zipPath);
return new ZipFile(new File(zipPath), ZipFile.OPEN_READ | ZipFile.OPEN_DELETE);
} catch (IOException | NullPointerException e) {
Timber.e(e, "Failed to download requested media files");
throw new RuntimeException(e);
} finally {
if (resp != null && resp.body() != null) {
resp.body().close();
}
}
}
use of com.ichi2.anki.exception.UnknownHttpResponseException in project AnkiChinaAndroid by ankichinateam.
the class RemoteMediaServer method begin.
public JSONObject begin() throws UnknownHttpResponseException, MediaSyncException {
try {
mPostVars = new HashMap<>();
mPostVars.put("k", mHKey);
mPostVars.put("v", String.format(Locale.US, "anki,%s,%s", VersionUtils.getPkgVersionNameFake(), Utils.platDesc()));
Response resp = super.req("begin", HttpSyncer.getInputStream(Utils.jsonToString(new JSONObject())));
JSONObject jresp = new JSONObject(resp.body().string());
JSONObject ret = _dataOnly(jresp, JSONObject.class);
mSKey = ret.getString("sk");
return ret;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of com.ichi2.anki.exception.UnknownHttpResponseException in project AnkiChinaAndroid by ankichinateam.
the class RemoteServer method hostKey.
/**
* Returns hkey or null if user/pw incorrect.
*/
@Override
public Response hostKey(String user, String pw) throws UnknownHttpResponseException {
try {
mPostVars = new HashMap<>();
JSONObject credentials = new JSONObject();
credentials.put("u", user);
credentials.put("p", pw);
return super.req("hostKey", HttpSyncer.getInputStream(Utils.jsonToString(credentials)));
} catch (JSONException e) {
return null;
}
}
Aggregations