我创建了一个简单的REST端点:
http://<server_address>:3000/sizes
Run Code Online (Sandbox Code Playgroud)
此URL返回一个包含json数组的非常简单的响应,如下所示:
[
{ "id": 1, "name": "Small", "active": true },
{ "id": 2, "name": "Medium", "active": true },
{ "id": 3, "name": "Large", "active": true }
]
Run Code Online (Sandbox Code Playgroud)
现在,我正在尝试使用GSON的Retrofit 2来使用此响应.
我添加了一个模型:
@lombok.AllArgsConstructor
@lombok.EqualsAndHashCode
@lombok.ToString
public class Size {
private int id;
private String name;
private boolean active;
@SerializedName("created_at")
private String createdAt;
@SerializedName("updated_at")
private String updatedAt;
}
Run Code Online (Sandbox Code Playgroud)
和服务:
public interface Service {
@GET("sizes")
Call<List<Size>> loadSizes();
}
Run Code Online (Sandbox Code Playgroud)
我已经实例化了一个改造:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://<server_address>:3000")
.addConverterFactory(GsonConverterFactory.create()) …Run Code Online (Sandbox Code Playgroud) 我偶然发现了一个非常有趣的Dependency Injection库ButterKnife.使用ButterKnife它可以轻松地将视图注入活动或片段.
class ExampleActivity extends Activity {
@InjectView(R.id.title) TextView title;
@InjectView(R.id.subtitle) TextView subtitle;
@InjectView(R.id.footer) TextView footer;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);
ButterKnife.inject(this);
// TODO Use "injected" views...
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果使用依赖注入这些观点必须public让Butterknife能注射它(使用private一个异常场的结果fields must not be private or static).
在我过去的项目中,我总是制作所有成员字段(包括视图),private因为我认为这是最佳实践(信息隐藏等)现在我想知道是否有理由不应该制作所有视图public?在这种情况下,我不能使用,ButterKnife但我想使用它,因为它简化了很多代码.
我正在使用一个开关,当打开时会触发警报并启动通知.
最初它工作正常并在设定的时间触发警报.手动更改时间后,它开始工作奇怪的通知是在开关打开但不在指定的设定时间时立即触发.
这是交换机的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
aSwitch = (Switch) findViewById(R.id.switch1);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
Boolean switchState = prefs.getBoolean("locked", false);
if(switchState) {
aSwitch.setChecked(true);
} else {
aSwitch.setChecked(false);
}
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
prefs.edit().putBoolean("locked", true).apply();
NotificationEventReceiver.setupAlarm(getApplicationContext());
aSwitch.setChecked(true);
} else {
prefs.edit().putBoolean("locked", false).apply();
NotificationEventReceiver.cancelAlarm(getApplicationContext());
aSwitch.setChecked(false);
}
}
});
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}
Run Code Online (Sandbox Code Playgroud)
当开关打开时,它将使用WakefullBroadcast接收器触发通知
.
这是接收器类的代码:
public class NotificationEventReceiver extends WakefulBroadcastReceiver {
private …Run Code Online (Sandbox Code Playgroud) 是否可以将默认组模型从"身份验证和授权"部分(在Django管理站点上)移动到自定义模型以及如何实现?
让我们从另一个词开始.
我的Django项目中有一个非常简单的应用程序'accounts'.
models.py文件如下所示:
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
def __str__(self):
return self.email
Run Code Online (Sandbox Code Playgroud)
serializers.py文件:
from rest_framework import serializers
from django.contrib.auth.models import Group
from django.contrib.auth import get_user_model
class GroupSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Group
class UserSerializer(serializers.HyperlinkedModelSerializer):
groups = serializers.HyperlinkedRelatedField(
many=True,
required=False,
read_only=True,
view_name="group-detail"
)
class Meta:
model = get_user_model()
exclude = ('user_permissions',)
Run Code Online (Sandbox Code Playgroud)
现在,在管理站点上,我有两个部分:"帐户"和"身份验证和授权"."帐户"部分包含我的"用户"表(用户模型)和"身份验证和授权"部分包含"组"表(适用于Django的默认授权组模型).
我的问题是 - 是否可以以及如何将Groups表(模型)移动到'Accounts'部分?
我甚至试图基于Django的默认auth Group模型创建一个自定义的"Group"模型,但一直坚持迁移异常.
我今天在开发Android应用程序时遇到了这种情况,我需要根据来自2个不同API的响应来呈现图形.我正在使用Volley而我所做的是我进行了顺序网络呼叫,即我发出了第一个请求,并且在该onResponse请求的方法中我发出了第二个请求.然后我在onResponse第二个请求的方法中渲染视图(图形).
现在我想优化这种情况.我想知道一种方法,我可以异步地进行这两个网络调用,我只在收到来自两个API的响应后呈现视图.所以,我说有3种模块化方法,即 -
loadView (根据从2个网络呼叫收到的数据渲染图表)我该怎么办呢?有人可以对它嗤之以鼻吗?
我使用Samsung Pass SDK(1.20)来验证拥有带指纹扫描仪的Samsung Android设备的用户.我需要让用户输入他/她的密码作为指纹扫描的替代方法(与Android锁屏相同).
在运行Android 5的三星设备中 - 它运行正常.该对话框显示一个带有"备份密码"文本的按钮,用户可以输入密码进行身份验证,但在Android 6.0及更高版本中 - 它不起作用.
使用该函数isFeatureEnabled(DEVICE_FINGERPRINT_AVAILABLE_PASSWORD)返回false.
看起来三星已在Android 6设备中禁用此功能.我在文档中找不到任何关于此的内容.
这有什么理由吗?更重要的是,有没有任何解决方法(当然不是黑客)?
谢谢!
正如问题所述 - 我正在尝试重命名我的API中公开的过滤器字段名称.
我有以下型号:
class Championship(Model):
...
class Group(Model):
championship = ForeignKey(Championship, ...)
class Match(Model):
group = ForeignKey(Group, ...)
Run Code Online (Sandbox Code Playgroud)
我在REST API中公开了所有这些模型.我filter_fields为Match模型定义了:
class MatchViewSet(ModelViewSet):
filter_fields = ['group__championship']
...
Run Code Online (Sandbox Code Playgroud)
这样,我可以过滤特定锦标赛的比赛(测试和工作):
curl/api/matches /?group__championship = 1
是否可以为暴露的过滤器使用某种别名,以便我可以使用以下内容:
curl/api/matches /?championship = 1
其中,championship在这种情况下将是一个别名group__championship?
pip freeze 收益:
django-filter==0.15.2
(...)
Run Code Online (Sandbox Code Playgroud)
我也试着实现自定义FilterSet与ModelChoiceFilter和自定义查询方法:
class MatchFilterSet(FilterSet):
championship = ModelChoiceFilter(method='filter_championship')
def filter_championship(self, queryset, name, value):
return queryset.filter(group__championship=value)
class Meta:
model = Match
fields = ['championship']
Run Code Online (Sandbox Code Playgroud)
有了观点:
class MatchViewSet(ModelViewSet):
filter …Run Code Online (Sandbox Code Playgroud) 我从 Android Studio Bumblebee 创建了一个 Android 项目,并尝试将 Firebase 集成到其中。根据 Firebase 的文档,我应该添加一些项目级别和模块级别的依赖项,如下所示。

当我转到项目级别 Gradle 文件时,它是这样的:
plugins {
id 'com.android.application' version '7.1.2' apply false
id 'com.android.library' version '7.1.2' apply false
id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,我应该在哪里添加这些依赖项?
我在基于 LibGDX 的游戏中注入一些依赖项时遇到问题。谁能指出我缺少什么?
我有两个模块。
首先提供Android的Context:
@Module
public class AppModule {
Context context;
public AppModule(Context context) {
this.context = context;
}
@Provides
@Singleton
Context providesContext() {
return context;
}
}
Run Code Online (Sandbox Code Playgroud)
第二个提供与 Google Analytics 交互的类:
@Module
public class ServicesModule {
@Provides
@Singleton
AnalyticsUtils providesAnalyticsUtils(Context context) {
return new AnalyticsUtils(context);
}
}
Run Code Online (Sandbox Code Playgroud)
我的组件类是这样实现的:
@Singleton
@Component(modules = {AppModule.class, ServicesModule.class})
public interface GameComponent {
void inject(Launcher launcher);
}
Run Code Online (Sandbox Code Playgroud)
现在,我添加了自定义应用程序类(在清单中定义),在其中实例化了我的组件:
public class GameApplication extends Application {
private GameComponent gameComponent;
@Override
public void onCreate() { …Run Code Online (Sandbox Code Playgroud) 我有一个像这样的 JSON:
{
"aggregation": {
"CityAgg" : {
"key" : "Paris"
}
}
}
Run Code Online (Sandbox Code Playgroud)
我创建映射,并为每个字段添加 a ,@SerializedName因为我想为变量创建自定义名称。
例如,在 JSON 中,有一个键名称key,但我希望 Java 中的变量是cityName。
所以,我这样做:
@SerializedName("key")
public String cityName
Run Code Online (Sandbox Code Playgroud)
我可以将响应 JSON 动态映射到我的对象,如下所示:
Gson gson = new Gson();
Query2 query2 = gson.fromJson(response, Query2.class);
Run Code Online (Sandbox Code Playgroud)
它工作得很好。
但是,当我想打印映射的对象时,我会这样做:
String query2String = gson.toJson(query2);
Gson gsonPretty = new GsonBuilder().setPrettyPrinting().create();
String prettyJson = gsonPretty.toJson(query2String);
Run Code Online (Sandbox Code Playgroud)
问题是,在 中prettyJson,我可以看到key,而不是cityName。
我想知道是否有办法定制它。我不想看到key。
我想在我的Android工作室项目中添加https://github.com/jkwiecien/EasyImage.当我添加行:
compile 'com.github.jkwiecien:EasyImage:1.2.1'
Run Code Online (Sandbox Code Playgroud)
到我的应用程序的build.gradle文件,它给了我:
错误(33,13)无法解决.
android ×8
java ×4
django ×2
gradle ×2
gson ×2
json ×2
python ×2
rest ×2
admin ×1
asynchronous ×1
butterknife ×1
dagger-2 ×1
fingerprint ×1
firebase ×1
libgdx ×1
local ×1
model ×1
retrofit2 ×1
sequential ×1