我正在使用Kotlin和Realm编写Android应用程序.我有一个JSONArray,我想迭代这个数组中的JSONObjects,以便在Realm数据库类中加载它们:
领域类:
import io.realm.RealmObject
import io.realm.annotations.PrimaryKey
import io.realm.annotations.Required
open class Person(
@PrimaryKey open var id: Long = 0,
@Required
open var name: String = ""
) : RealmObject() {
}
Run Code Online (Sandbox Code Playgroud)
JSONArray:
{
"persons":[
{
"id":0,
"name":"Biatrix"
},
{
"id":1,
"name":"Bill"
},
{
"id":2,
"name":"Oren"
},
{
"id":3,
"name":"Budd"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我试过迭代如下:
for (item : JSONObject in persons) {
}
Run Code Online (Sandbox Code Playgroud)
......但是我收到了for-loop range must have an iterator() method错误.
我正在将应用升级到org.apache.http不推荐使用的API 23 .
我当前(已弃用)的代码如下所示:
HttpClient httpClient = new DefaultHttpClient();
File file = new File(attr.Value);
String url = server_url;
HttpPost request = new HttpPost(url);
FileEntity fileEntity = new FileEntity(file, "image/png");
request.setEntity(fileEntity);
HttpResponse response = httpClient.execute(request);
String output = getContent(response.getEntity().getContent());
Run Code Online (Sandbox Code Playgroud)
我已经找到了一些关于如何使用它的建议HttpURLConnection,但它们比当前的解决方案(不能再使用)复杂得多.我正在谈论用于执行与上述相同功能的许多代码行.
有没有人有一个很好的固定更短的解决方案呢?
java android httpurlconnection apache-commons-httpclient android-6.0-marshmallow
我一直在尝试将Crashlytics添加到我正在Android Studio 1.1.0上开发的应用程序中,但没有运气.我已将Fabric插件添加到Android Studio,在添加我的应用时,无法识别Crashlytics库.
这是Fabric添加的代码:
...
import com.crashlytics.android.Crashlytics; // Error here
import io.fabric.sdk.android.Fabric; // Error here
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fabric.with(this, new Crashlytics()); // Error here
setContentView(R.layout.activity_main);
...
}
Run Code Online (Sandbox Code Playgroud)
以下是错误:
C:\Workspace\Projects\AndroidG\Borgertip\borgertip\src\main\java\dk\gis34\borgertip\activity\MainActivity.java
Error:(12, 31) error: package com.crashlytics.android does not exist
Error:(16, 29) error: package io.fabric.sdk.android does not exist
Error:(67, 31) error: cannot find symbol class Crashlytics
Error:(67, 9) error: cannot find symbol variable Fabric
Run Code Online (Sandbox Code Playgroud)
一个善良的灵魂可以说明为什么这不仅仅起作用,就像它在Eclipse上一样?
我将在周末发布通知。如果在一段时间后未看到/点击通知,我希望它自行消失。我找不到在NotificationCompat.Builder表达式中设置超时或类似内容的任何方法。这是(非常正常的通知生成器)代码:
Intent myIntent = new Intent(context, MyActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.not_bar_small_icon)
.setColor(ContextCompat.getColor(context, R.color.Green))
.setContentTitle(context.getResources().getString(R.string.strNotificationTitle))
.setContentText(context.getResources().getString(R.string.strNotificationMessage))
.setContentIntent(pendingIntent);
NotificationManager notifyManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
notifyManager.notify(MY_NOTIFICATION_ID, mBuilder.build());
Run Code Online (Sandbox Code Playgroud)
这能做到吗?
解决方案更新:
使用下面评论中建议的 Aleksandar 链接解决。
在我们的应用程序中,用户多年来一直使用(大约)以下代码上传数百万个图像:
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(postFilePath, bmOptions);
Bitmap roughBitmap = BitmapFactory.decodeFile(postFilePath, bmOptions);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
roughBitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);
InputStream fis = new ByteArrayInputStream(stream.toByteArray());
int fileSize = stream.toByteArray().length;
conn.setRequestProperty("Content-Length", Integer.toString(fileSize));
conn.setFixedLengthStreamingMode(fileSize);
...
if (fis != null) {
byte[] buf = new byte[10240];
int read;
while ((read = fis.read(buf)) > 0) {
os.write(buf, 0, read);
totalBytesRead += read;
if (uploadProgressListener != null) {
try {
uploadProgressListener.onBytesUploaded(read);
} catch (Exception e) {
Log.e(e);
}
}
}
fis.close(); …Run Code Online (Sandbox Code Playgroud) 我有一个具有所有功能和一些活动的库项目,并且还有一个包装器活动,该包装器活动仅具有JSON配置字符串和某些样式。我将库导入了包装器项目,然后在包装器项目中将库的活动之一设置为启动活动,但随后收到一个错误消息,即从库中选择的活动不是活动子类或别名。
这是什么意思,我可以纠正吗?
应该启动的活动:
package dk.borgertip.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import dk.borgertip.R;
public class SplashActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Thread startTimer = new Thread() {
@Override
public void run() {
try {
sleep(3000);
Intent i = new Intent(SplashActivity.this, MainActivity.class);
startActivity(i);
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
startTimer.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to …Run Code Online (Sandbox Code Playgroud) 我有一个表'新娘',有一些像这样的字符串值:
id name order
------- ------- -------
1 Beatrix
2 Bill
3 ORen
4 Vernita
5 Elle
6 Budd
Run Code Online (Sandbox Code Playgroud)
我想根据排序'name'列的顺序,用数字(从1开始)更新列'order'.像那样:
id name order
------- ------- -------
1 Beatrix 1
2 Bill 2
3 ORen 5
4 Vernita 6
5 Elle 4
6 Budd 3
Run Code Online (Sandbox Code Playgroud)
我的问题是如何使用排序的名称作为插入实际订单号的顺序.任何帮助都会很棒.
我在 Fragment 伴随对象中定义了一个枚举:
enum class Channel {
TAG_FRIENDS, CHALLENGE_INVITE, COMMITMENT_INVITE
}
Run Code Online (Sandbox Code Playgroud)
通常我会从另一个 Kotlin 类访问它,如下所示:
MyFragment.Companion.Channel.TAG_FRIENDS
Run Code Online (Sandbox Code Playgroud)
我如何从 Java 类访问它?枚举Channel似乎无法访问......
我一直在尝试以与@Erwin Brandstetter用可接受的答案回答几次相同的方式从 XML 中提取值,但它对我不起作用:
根据这篇文章和这篇文章,这应该可以工作,但我只是得到一个空结果:
WITH x AS ( SELECT
'<Attributes xmlns="http://www.gis34.dk">
<bemaerkning displayName="Bemærkning">Beatrix</bemaerkning>
<billede displayName="Billede">https://vignette.wikia.nocookie.net/killbill/images/3/39/The_Bride.jpg</billede>
</Attributes>'::xml AS t
)
SELECT xpath('/Attributes/bemaerkning/text()', t) as comment
FROM x
Run Code Online (Sandbox Code Playgroud)
结果:(预期:{我的评论})
comment
xml[]
-------
{}
Run Code Online (Sandbox Code Playgroud)
我的数据库版本:
PostgreSQL 9.1.3 on x86_64-unknown-linux-gnu, compiled by gcc-4.5.real (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2, 64-bit
Run Code Online (Sandbox Code Playgroud)
有人有想法吗?
问题很简单:在 Kotlin 中,当我arguments使用 a实例化片段时Bundle(),系统仍然需要使用 来保证参数对象!!。现在arguments应该绝对不是空的,对吧?那为什么需要呢?
这是代码:
private fun openPinCodeFragment(mode: PinView.Mode) {
currentFragment = PinCodeFragment()
currentFragment?.run {
arguments = Bundle()
arguments!!.putSerializable(MODE, mode)
}
openFragment(currentFragment)
}
Run Code Online (Sandbox Code Playgroud)
如果我删除!!那么:
android ×6
kotlin ×3
java ×2
postgresql ×2
crashlytics ×1
exif ×1
json ×1
sql ×1
sql-update ×1
xpath ×1