我目前的代码:
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
Mall[] malls = restTemplate.getForObject(url, Mall[].class);
Run Code Online (Sandbox Code Playgroud)
我需要在表单中为我的请求添加一些自定义标头:
X-TP-DeviceID : <GUID>
Run Code Online (Sandbox Code Playgroud)
在我的案例中,最简单的方法是什么?restTemplate
在将请求发送到服务器之前,有没有办法向我的对象添加自定义标头定义?
[编辑]
这是对的吗?
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
HttpHeaders headers = new HttpHeaders();
headers.set("X-TP-DeviceID", "1234567890");
HttpEntity entity = new HttpEntity(headers);
HttpEntity<Mall[]> response = restTemplate.exchange(url, HttpMethod.GET, entity, Mall[].class);
Mall[] malls = response.getBody();
Run Code Online (Sandbox Code Playgroud)
[添加]
所以,我设法让它工作.但是,我对此并不完全满意.在我的情况下,我将需要为我所做的所有调用提供相同的自定义标头.
所以,我的下一个问题是 - 是否可以设置我的自定义标头在每次web-service
调用时自动添加,例如,通过扩展RestTemplate
类并将所有自定义标头放在那里?然后,我需要做的就是简单地使用我的自定义扩展RestTemplate
而不是库存扩展,默认情况下我的所有自定义标头都会出现在那里.
示例ViewModel:
public class NameViewModel extends ViewModel {
// Create a LiveData with a String
private MutableLiveData<String> mCurrentName;
public MutableLiveData<String> getCurrentName() {
if (mCurrentName == null) {
mCurrentName = new MutableLiveData<>();
}
return mCurrentName;
}
}
Run Code Online (Sandbox Code Playgroud)
主要活动:
mModel = ViewModelProviders.of(this).get(NameViewModel.class);
// Create the observer which updates the UI.
final Observer<String> nameObserver = textView::setText;
// Observe the LiveData, passing in this activity as the LifecycleOwner and the observer.
mModel.getCurrentName().observe(this, nameObserver);
Run Code Online (Sandbox Code Playgroud)
我想调用mModel.getCurrentName().setValue(anotherName);
第二个活动并使MainActivity接收更改.那可能吗?
我有以下代码(示例):
public dynamic GetData(string name)
{
using(var ctx = GetObjectContext())
{
switch (name)
{
case "entity1":
return ctx.entity1.ToList();
case "entity2":
return ctx.entity2.ToList();
......
default:
return null;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想避免在此示例中切换.如何按名称查找所需的实体类,调用ToList()方法并返回数据?我可以用反射做到这一点吗?你能帮助我吗?
请看这个简单的代码:
final String url = String.format("%s/api/shop", Global.webserviceUrl);
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
HttpHeaders headers = new HttpHeaders();
headers.set("X-TP-DeviceID", Global.deviceID);
HttpEntity entity = new HttpEntity(headers);
HttpEntity<Shop[]> response = restTemplate.exchange(url, HttpMethod.GET, entity, Shop[].class);
shops = response.getBody();
Run Code Online (Sandbox Code Playgroud)
如您所见,上面的代码旨在从服务器(以json格式)获取商店列表,并将响应映射到Shop对象数组.现在我需要推出新店,例如/ api/shop/1.请求实体应具有与返回的格式完全相同的格式.
我应该将/ 1添加到我的网址,创建新的Shop类对象,所有字段都填充了我想要放置的值,然后使用与HttpMethod.PUT的交换?
请为我澄清一下,我是Spring的初学者.代码示例将不胜感激.
[编辑]我很困惑,因为我刚注意到方法RestTemplate.put().那么,我应该使用哪一个?交换还是放()?
在搜索官方文档时我无法找到任何方法来做到这一点。有人知道有什么方法可以做到这一点吗?
我有用wix创建的独立安装项目.我需要一些解决方案来自动更新我的应用程序.我的应用程序shuold在启动时检查新版本并自动下载并安装新版本(如果可用).
这样做的最佳解决方案是什么?在wix中有点像ClickOnce吗?ClickThrough似乎已经死了,对吗?
因此,参数名称在方法调用中可见.这很酷,但正如您所看到的,在这种情况下,只有第一个参数,第二个参数没有可见的名称.根据我的观察,它有时是有效的,有时却没有,我无法弄清楚为什么.
我可以配置它,还是只是一个bug?或许我不明白该功能是如何工作的.
Android Studio 3.1.1.新模块 - >导入gradle项目,选择要导入和确认的项目后,IDE崩溃异常:
元素:类org.jetbrains.plugins.groovy.lang.psi.impl.GroovyFileImpl因为:不同的提供者:SingleRootFileViewProvider {myVirtualFile = file:///home/michal/AndroidStudioProjects/FirebaseJobTest/settings.gradle,content = VirtualFileContent {size = 26}}(1416c50); SingleRootFileViewProvider {myVirtualFile = file:///home/michal/AndroidStudioProjects/FirebaseJobTest/settings.gradle,content = VirtualFileContent {size = 26}}(665cfacf)在以下位置失效:请参阅附件com.intellij.psi.PsiInvalidElementAccessException:Element:class org .jetbrains.plugins.groovy.lang.psi.impl.GroovyFileImpl因为:不同的提供者:SingleRootFileViewProvider {myVirtualFile = file:///home/michal/AndroidStudioProjects/FirebaseJobTest/settings.gradle,content = VirtualFileContent {size = 26}}( 1416c50); SingleRootFileViewProvider {myVirtualFile = file:///home/michal/AndroidStudioProjects/FirebaseJobTest/settings.gradle,content = VirtualFileContent {size = 26}}(665cfacf)在以下位置无效:请参阅com.intellij.psi.util.PsiUtilCore.ensureValid中的附件(PsiUtilCore.java:502)
你知道任何解决方法吗?
我按照此处的建议对 Android 清单进行了更改: https ://docs.flutter.dev/development/ui/navigation/deep-linking
因此,新的元数据和配置已添加到 MainActivity 中:
<!-- Deep linking -->
<meta-data android:name="flutter_deeplinking_enabled" android:value="true" />
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" android:host="flutterbooksample.com" />
<data android:scheme="https" />
</intent-filter>
Run Code Online (Sandbox Code Playgroud)
结果,当我按照他们所说的那样使用 adb 进行测试时:
adb shell am start -a android.intent.action.VIEW \
-c android.intent.category.BROWSABLE \
-d "http://flutterbooksample.com/book/1"
Run Code Online (Sandbox Code Playgroud)
我得到了我想要的:
但是,如果我尝试http://flutterbooksample.com/book/1
直接在模拟器上的浏览器中打开 url,则什么也不会发生,浏览器打开页面,并且没有提示在我的应用程序中打开 url。
我还需要做其他事情才能使其正常工作吗?
我的应用程序的目标API是27(奥利奥).我的应用程序使用EditText控件进行文本输入.但是,从Google键盘,现在可以插入GIF.在这种情况下,我的应用程序接收ACTION_SEND意图,从键盘插入gif,这是为完全不同的功能实现的.因此,它只会混淆整个应用流程,并使用户完全不同的活动.
我希望我的EditText只接受文本,不允许用户在那里插入任何gif.如何配置EditText以停止允许从键盘插入GIFS?