我需要能够 使用OKHTTP 管理一些请求,使用通过键入地址来接收一些预测.该问题是每当我插入时间CHAR它将使一个新的请求,但在同一时间,我需要取消前一个!例如:同时纽约市= 13个请求!所以我正在使用单个实例来尝试取消已经请求但没有成功的任何内容.这就是我做的!Google Places AutoComplete
Call
Address.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(Address.getText().toString().length() > 3){
_Address = Address.getText().toString();
if(call != null){
call.cancel();
}
Request request = new Request.Builder()
.url(getPlaceAutoCompleteUrl(_Address))
.addHeader("content-type", "application/json")
.addHeader("User-Agent", "Mozilla/5.0 (Linux; U; Android 4.0.3; en-us; google_sdk Build/MR1) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30")
.build();
call = client.newCall(request);
call.enqueue(new Callback() {
public void onResponse(Call call, final Response response) throws IOException …
Run Code Online (Sandbox Code Playgroud) 我读到线程OkHttpClient无法取消“按标签调用”,但是它对我不起作用。我有一个EditText
with TextWatcher
可以使用Google PlacesAutocomplete获取位置地址,因此键入的每个字符都是一个新请求,但是我键入的每个字符都需要取消上一个请求,因此我不会收到它的返回,所以这很重要是键入的最终地址,我这样做是:
更新!
Address.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(Address.getText().toString().length() > 3){
_Address = Address.getText().toString();
if(call != null){
call.cancel();
}
Request request = new Request.Builder()
.url(getPlaceAutoCompleteUrl(_Address))
.addHeader("content-type", "application/json")
.addHeader("User-Agent", "Mozilla/5.0 (Linux; U; Android 4.0.3; en-us; google_sdk Build/MR1) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30")
.build();
Call call = new OkHttpClient().newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void …
Run Code Online (Sandbox Code Playgroud) 我是Dapper的初学者,对最佳做法有一些疑问。我的项目是Asp.net WebApi。
打开连接字符串
在此线程中,在Controller内以这种方式打开了与数据库的连接,但这是一个简单的项目,并不意味着要成为WebService:
static IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlServerConnString"].ConnectionString);
Run Code Online (Sandbox Code Playgroud)
但是我发现其他带有using
声明的例子:
using (IDbConnection connection = new SqlConnection(stringConnection))
{
//do something
}
Run Code Online (Sandbox Code Playgroud)
由于此项目是WebApi,因此using语句会更好,因为Dispose
请求会更好吗?
上市数据
在上面的同一线程中,显示了如何基于static IDbConnection db
property 检索列表:
var res = (List<ShippDetails>)db.Query<ShippDetails>(query, new { id });
Run Code Online (Sandbox Code Playgroud)
还是会更好用.AsList()
?
var res = connection.Query<ShippDetails>(query, new { id }).AsList();
Run Code Online (Sandbox Code Playgroud)
控制器的动作
对于我所有的动作,它看起来像:
[Route("FF")]
[HttpGet]
public async Task<HttpResponseMessage> get()
{
var response = new HttpResponseMessage();
int id = 1;
var res = (List<ShippDetails>)db.Query<ShippDetails>(query, new { id }); …
Run Code Online (Sandbox Code Playgroud) 我正在将 Google Autocomplete Places API 与 android 一起使用,我想要做的是:
这就是为什么我需要一个单例实例,我试过这个:
public class OkHttpSingleton extends OkHttpClient {
private static OkHttpClient client = new OkHttpClient();
public static OkHttpClient getInstance() {
return client;
}
public OkHttpSingleton() {}
public void CloseConnections(){
client.dispatcher().cancelAll();
}
public List<PlacePredictions> getPredictions(){
//// TODO: 26/09/2017 do the request!
return null;
}
Run Code Online (Sandbox Code Playgroud)
}
但我不确定这是否是正确的方法,因为在文档中它说该dispatcher().cancelAll()
方法取消所有请求但我知道这种方法是错误的!我更关心如何创建一个单例然后其余的。
主要活动:
Address.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { …
Run Code Online (Sandbox Code Playgroud)