我正在尝试使用Dagger 2使用Retrofit 2.0执行登录操作
这是我如何设置Retrofit依赖
@Provides
@Singleton
Retrofit provideRetrofit(Gson gson, OkHttpClient client) {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson)
.client(client)
.baseUrl(application.getUrl())
.build();
return retrofit;
}
Run Code Online (Sandbox Code Playgroud)
这是API接口.
interface LoginAPI {
@GET(relative_path)
Call<Boolean> logMe();
}
Run Code Online (Sandbox Code Playgroud)
我有三个不同的基本网址用户可以登录.因此,在设置Retrofit依赖项时,我无法设置静态URL.我在Application类上创建了一个setUrl()和getUrl()方法.用户登录后,我在调用API调用之前将url设置为Application.
我像这样使用懒惰注射进行改造
Lazy<Retrofit> retrofit
Run Code Online (Sandbox Code Playgroud)
这样,只有当我可以打电话时,Dagger才会注入依赖关系
retrofit.get()
Run Code Online (Sandbox Code Playgroud)
这部分效果很好.我把url设置为改进依赖.但是,当用户键入错误的基本URL(例如,mywifi.domain.com)时,会出现问题,理解它是错误的并更改它(比如mydata.domain.com).由于Dagger已经为改造创建了依赖关系,因此它不会再做了.所以我必须重新打开应用程序并输入正确的URL.
我阅读了不同帖子,使用Dagger在Retrofit上设置动态网址.在我的情况下,没有什么比这更好的了.我想念什么吗?
我正在尝试使用URLConnection下载pdf文件.这是我设置连接对象的方法.
URL serverUrl = new URL(url);
urlConnection = (HttpURLConnection) serverUrl.openConnection();
urlConnection.setDoInput(true);
urlConnection.setRequestMethod("GET");
urlConnection.setRequestProperty("Content-Type", "application/pdf");
urlConnection.setRequestProperty("ENCTYPE", "multipart/form-data");
String contentLength = urlConnection.getHeaderField("Content-Length");
Run Code Online (Sandbox Code Playgroud)
我从连接对象获得了输入流.
bufferedInputStream = new BufferedInputStream(urlConnection.getInputStream());
Run Code Online (Sandbox Code Playgroud)
并输出流来写入文件内容.
File dir = new File(context.getFilesDir(), mFolder);
if(!dir.exists()) dir.mkdir();
final File f = new File(dir, String.valueOf(documentName));
f.createNewFile();
final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(f, true)); //true for appendMode
Run Code Online (Sandbox Code Playgroud)
创建BlockingQueue,以便执行读写操作的线程可以访问队列.
final BlockingQueue<ByteArrayWrapper> blockingQueue = new ArrayBlockingQueue<ByteArrayWrapper>(MAX_VALUE,true);
final byte[] dataBuffer = new byte[MAX_VALUE];
Run Code Online (Sandbox Code Playgroud)
现在创建了从InputStream读取数据的线程.
Thread readerThread = new Thread(new Runnable() {
@Override
public void run() {
try …Run Code Online (Sandbox Code Playgroud) 我正在使用CheckBoxPreference设置屏幕.XML是:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference android:key="includeAddress"
android:title="Include Address"
android:summary=""
android:defaultValue="true" />
<CheckBoxPreference android:key="rememberName"
android:title="Remeber Name"
android:summary=""
android:defaultValue="false" />
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)
我在应用程序中更改了值.用户注销后,必须将其设置为xml中定义的默认值.但是,它似乎不起作用.他们保留我最后选择的那些价值观.
阅读Android文档后,我发现了这个:
PreferenceManager.setDefaultValues(getApplicationContext(), R.xml.preference_settings, true);
Run Code Online (Sandbox Code Playgroud)
但它几乎没有工作!使用SharedPreferences尝试其他方式.
SharedPreferences preferences = getParent().getSharedPreferences("preference_settings", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.commit();
Run Code Online (Sandbox Code Playgroud)
它也没用!
我错过了什么吗?如何将首选项设置为XML中定义的默认值?
提前致谢!
我正在处理的应用程序主要基于操作从服务器获取的JSON数据.传统的JSON解析器提取值,设置所需的POJO并传递给UI处理程序进行渲染.这部分现在运作良好.
我听说过GSON库并执行其实现步骤.根据我的理解,它(GSON使用)需要以下内容.
上述方法听起来更像是对象映射.但是,我不知道GSON与老式JSON解析相比效率如何; 特别是复杂的JSON.它对内存使用的影响?
你怎么看?
我有以下协程,它调用挂起函数,然后添加延迟并向用户显示结果
CoroutineScope(Dispatchers.Main).launch {
//some initial setup
val result = service.getResult() //suspend function
//add a delay of 1s
delay(1000)
when(result) {
ERROR -> {
Toast.makeText(activity, message, Toast.LENGTH_LONG).show()
}
//other conditions
}
}
Run Code Online (Sandbox Code Playgroud)
当我添加延迟时,它下面的代码永远不会运行。作为一个挂起函数,延迟应该起作用,然后必须执行其余代码。不确定出了什么问题。有什么线索吗?

我想在安装程序窗口中更改应用程序名称和标签(请参阅附图).默认情况下,名称和标签是包名称.即使我在AndroidManifest.xml中添加了自定义名称和标签,它也不会反映在此安装程序窗口中.
AndroidManifest.xml如下所示.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.style"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".StylePageActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
strings.xml(或/res/values/base-strings.xml)中的自定义名称是
<string name="app_name">Demo Application</string>
Run Code Online (Sandbox Code Playgroud) 谁能告诉我这里出了什么问题?
public class BackgroungTask extends AsyncTask<String, Void, Void> {
public Void doInBackground(String... params) {
//tasks
return; //error occurs here!
}
public void onPostExecute(Void result) {
//codes
}
}
Run Code Online (Sandbox Code Playgroud)
上述类不依赖于返回值.所以onPostExceute()只是执行那里写的代码.
提前致谢!
我正在使用Google Cloud Messaging(GCM)处理推送通知.我可以按照本教程设置服务器和客户端.
服务器在Apache Tomcat6.0(localhost)上运行,在Android API(API级别17)上运行Android模拟器.我有发件人ID和API密钥.从模拟器运行时,我获得了成功的设备连接消息.在此之后,从服务器发送消息后显示以下错误.
com.google.android.gcm.server.InvalidRequestException: HTTP Status Code: 401()
com.google.android.gcm.server.Sender.sendNoRetry(Sender.java:211)
com.google.android.gcm.server.Sender.send(Sender.java:125)
com.google.android.gcm.demo.server.SendAllMessagesServlet.doPost(SendAllMessagesServlet.java:83)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
Run Code Online (Sandbox Code Playgroud)
从其他帖子,我理解错误的API密钥是罪魁祸首.但就我而言,我确保使用API控制台中正确的一个.
顺便说一句,当我运行模拟器时,我看到一条消息'将regId发送到服务器'.什么是注册ID?它与发件人ID不同; 看起来像编码的.
有突破吗?
编辑
问题终于解决了!Ant没有正确构建WAR文件.因此API密钥几乎不会更新.发现这个虫子真是一场噩梦.谢谢大家的投入!
我有一个包含文本视图和复选框的线性布局。
<LinearLayout style="@style/linearLayoutStyleNoBgColor" >
<TextView
android:id="@+id/useFollowupDate"
style="@style/textFieldStyleType1WithCheckbox" />
<CheckBox
android:id="@+id/checkFollowDate"
style="@style/checkboxStyleType1"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
样式是:
<style name="linearLayoutStyleNoBgColor">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
<item name="android:layout_margin">5dp</item>
<item name="android:weightSum">1.0</item>
</style>
<style name="textFieldStyleType1WithCheckbox" parent="@style/textFieldStyleType1">
<item name="android:layout_height">match_parent</item>
<item name="android:layout_weight">0.30</item>
<item name="android:gravity">center_vertical</item>
</style>
<style name="checkboxStyleType1">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_weight">0.70</item>
<item name="android:gravity">right|end</item>
</style>
Run Code Online (Sandbox Code Playgroud)
我想要实现的是
TextView | checkbox
(30%percent screen) | (to the right)
Run Code Online (Sandbox Code Playgroud)
但我现在得到的是
TextView | checkbox
(30%percent screen) | (left aligned)
Run Code Online (Sandbox Code Playgroud)
我哪里错了?
android android-layout android-linearlayout android-checkbox android-styles
我试图捕捉箭头键(向上,向下,向右和向左)生成的事件并禁用它们.下面的代码片段来自其中一个活动类.
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.KEYCODE_DPAD_DOWN) return true;
else return true;
}
Run Code Online (Sandbox Code Playgroud)
但是,使用这些代码,密钥导航正在运行.我尝试将键监听器添加到不起作用的活动.
目标设备是搭载Android 2.2版本的三星GT-I5500.
我错过了什么吗?