所以,当我使用Koush的Ion时,我能够通过一个简单的方式在我的帖子中添加一个json体 .setJsonObjectBody(json).asJsonObject()
我正在转向OkHttp,我真的没有看到一个很好的方法来做到这一点.我到处都收到错误400.
有人有主意吗?
我甚至尝试将其手动格式化为json字符串.
String reason = menuItem.getTitle().toString();
JsonObject json = new JsonObject();
json.addProperty("Reason", reason);
String url = mBaseUrl + "/" + id + "/report";
Request request = new Request.Builder()
.header("X-Client-Type", "Android")
.url(url)
.post(RequestBody
.create(MediaType
.parse("application/json"),
"{\"Reason\": \"" + reason + "\"}"
))
.build();
client.newCall(request).enqueue(new com.squareup.okhttp.Callback() {
@Override
public void onFailure(Request request, IOException throwable) {
throwable.printStackTrace();
}
@Override
public void onResponse(Response response) throws IOException {
if (!response.isSuccessful()) throw new IOException(
"Unexpected code " + response);
runOnUiThread(new Runnable() {
@Override …Run Code Online (Sandbox Code Playgroud) 无法找到在占位符中加载gif图像的完美解决方案
Glide
.with(context)
.load("imageUrl")
.asGif()
.placeholder(R.drawable.gifImage)
.crossFade()
.into(imageView)
Run Code Online (Sandbox Code Playgroud)
尝试了Glide版本3.7.0的asGif()属性.但没有运气!
我正在使用离子库,我应该说这很棒.但我找不到创建PUT,DELETE,GET请求的文档?如果它可以处理这个REST请求你能指导我吗?
使用此网络库:
由于目前的状态是开发,我想使用自签名SSL证书
图书馆论坛有一些讨论:
https://github.com/koush/ion/issues/3
Ion ion = Ion.getDefault(c);
ion.configure().createSSLContext("TLS");
ion.getHttpClient().getSSLSocketMiddleware().setSSLContext(sslContext);
ion.getHttpClient().getSSLSocketMiddleware().setTrustManagers(trustManagers);
Run Code Online (Sandbox Code Playgroud)
经过一些研究,我抓住了crt并获得了sslContext和trustmanager,问题是,它仍然返回异常
javax.net.ssl.SSLException
Caused by: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
Run Code Online (Sandbox Code Playgroud)
这是我的尝试:
try {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream caInput = getResources().openRawResource(R.raw.load);
Certificate ca;
try {
ca = cf.generateCertificate(caInput);
//System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
} finally {
caInput.close();
}
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);
ssl_context = SSLContext.getInstance("TLS");
ssl_context.init(null, tmf.getTrustManagers(), null);
} catch (Exception …Run Code Online (Sandbox Code Playgroud) 我对泛型方法的显式类型参数有问题.我知道我可以这样做:
Foo.<Bar>function();
Run Code Online (Sandbox Code Playgroud)
假设有一个
void <T> function() {...}
Run Code Online (Sandbox Code Playgroud)
在Foo类中的函数.确切的问题是:
我想下载一些内容(带有Ion的 Android )
这些内容类似(Article,BlogArticle,...),都实现了ContentItem接口
目前下载如下:
例如新闻
private void downloadNews() {
Ion.with(this)
.load(URL_NEWS)
.as(new TypeToken<List<Article>>(){})
.setCallback(new FutureCallback<List<Article>>() {
@Override
public void onCompleted(Exception e, List<Article> result) {
// do something with result
}
});
}
Run Code Online (Sandbox Code Playgroud)
如果我想下载博客文章,我必须更改网址和文章类(对于BlogArticle).
我试着像这样制作一个通用函数:
private <T extends ContentItem> void download(String url) {
Ion.with(this)
.load(url)
.as(new TypeToken<List<T>>(){})
.setCallback(new FutureCallback<List<T>>() {
@Override
public void onCompleted(Exception e, List<T> result) {
// do something with result
}
});
}
Run Code Online (Sandbox Code Playgroud)
并调用该函数
this.<Article>download(url);
Run Code Online (Sandbox Code Playgroud)
没关系,编译得很好.跑完后我得到了
java.lang.ClassCastException:com.google.gson.internal.LinkedTreeMap无法强制转换为com.my.packagename.model.ContentItem
问题是它没有使用显式类将Json映射到pojo. …
我正在完成从库中挑选文件的任务,并将挑选的文件上传到服务器.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == PICK_FILE_REQUEST) {
if (data != null) {
//no data present
Uri uri = data.getData();
String filePath = data.getData().getPath();
// String path = uri.getPath();
file = new File(filePath);
String name = getContentName(getContentResolver(), uri);
try {
InputStream inStream = getContentResolver().openInputStream(uri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
} catch (IOException e) {
e.printStackTrace();
}
LinearLayout …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过 ionic (v5) 将 GitHub 徽标嵌入到我的项目中,该项目重定向到我制作的项目的 GitHub 存储库。
该徽标有一个令人讨厌的标题“ionicons-v5_logos”,当我将鼠标悬停在该图标上时会出现该标题。如何删除徽标的标题?我试图通过在 HTML 中编写以下代码来删除徽标的现有标题(但它似乎没有删除离子图标上的标题)-
<ion-icon name="logo-github" title="Check the code"></ion-icon>
Run Code Online (Sandbox Code Playgroud)
我尝试使用aria-label,aria-hidden和ariaLabel属性来设置/隐藏离子图标的标题,但没有任何效果。谁能指导我删除离子图标的标题?
我将像这样使用 Ion 将文件上传到 REST API(基于 Koush 提供的示例代码)
if (Upload != null && !Upload.isDone() && !Upload.isCancelled()){
resetUploadReq();
return;
}
String UploadUrl = Constants.Video_Upload_URL+userToken;
Upload = Ion.with(UploadActivity.this).load(UploadUrl)
.uploadProgressHandler(new ProgressCallback() {
@Override
public void onProgress(long uploaded, long total) {
int mProgress = (int) (100*uploaded / total);
mUploadBtn.setProgress(mProgress);
}
})
.setTimeout( 15 * 1000)
.setMultipartFile("file", new File(Path))
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception ex, JsonObject result) {
resetUploadReq();
if (ex == null){
JsonObject ReturnJson = result.getAsJsonObject();
Status = ReturnJson.get("status").getAsBoolean();
if (Status == …Run Code Online (Sandbox Code Playgroud) android file-upload multipartform-data timeoutexception android-ion
基本网址从 http:// 更改为 https:// 后,我突然遇到异常异常。每次我使用 https:// 点击 API 时,我都会看到日志闪烁“HTTP 失败:javax.net.ssl.SSLHandshakeException:java.security.cert.CertPathValidatorException:未找到证书路径的信任锚。” 这个错误。
我认为大多数时候都是 Retrofit 自动导入证书(因为我已经尝试使用 Retrofit 连接 https:// 多次。我之前没有遇到过这个问题)