从API Android的搜索建议建议、API、Android

2023-09-06 15:22:05 作者:时光可待你安好

我试图创建一个可搜索的活动,从谷歌的地方API得到它的结果,我创建了一个内容提供商,并把一些code ++做的HTTP请求谷歌和分析的结果。

I'm trying to create a searchable activity that gets its results from the google places API, I've created a content provider and put some code to do the HTTP request to google and parse the result.

问题是,网络请求需要异步完成,以阻止它阻塞UI线程,当这样的内容提供商做返回MatrixCursor Web请求已完成使结果出现下一次的文本前箱变的当文本实际上改变吧。

The problem is that the web request needs to be done asynchronously to stop it blocking the UI thread, when it is done like this the content provider returns the MatrixCursor before the web request has completed making the results appear the next time the text box changes instead of when the text actually changes.

有没有解决这个办法吗?

Is there any way around this?

下面是我的code为我的搜索内容提供商:

Here is my code for my search content provider:


import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

进口org.json.JSONArray;进口org.json.JSONException;进口org.json.JSONObject;

import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject;

进口android.app.SearchManager;进口android.content.ContentProvider;进口android.content.ContentValues​​;进口android.database.Cursor;进口android.database.MatrixCursor;进口android.net.Uri;进口android.util.Log;

import android.app.SearchManager; import android.content.ContentProvider; import android.content.ContentValues; import android.database.Cursor; import android.database.MatrixCursor; import android.net.Uri; import android.util.Log;

进口com.loopj.android.http。*;

import com.loopj.android.http.*;

公共类LocationsSuggestionProvider扩展ContentProvider的{    私有静态最后的String [] =柱{        _id,//必须包括此列        SearchManager.SUGGEST_COLUMN_TEXT_1};    公共MatrixCursor光标=新MatrixCursor(列);    公共LocationsSuggestionProvider()    {

public class LocationsSuggestionProvider extends ContentProvider { private static final String[] COLUMNS = { "_id", // must include this column SearchManager.SUGGEST_COLUMN_TEXT_1}; public MatrixCursor cursor = new MatrixCursor(COLUMNS); public LocationsSuggestionProvider() {

}
@Override
public int delete(Uri arg0, String arg1, String[] arg2) {
    return 0;
}

@Override
public String getType(Uri uri) {

    return null;
}

@Override
public Uri insert(Uri uri, ContentValues values) {
    return null;
}

@Override
public boolean onCreate() {
    return false;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    if(selectionArgs[0].length() >= 2 && selectionArgs[0].length() < 75)
    {
        AsyncHttpClient client = new AsyncHttpClient();
        try {
            Log.d("Bustimes","https://maps.googleapis.com/maps/api/place/autocomplete/json?input="+ URLEncoder.encode(selectionArgs[0].trim(), "UTF-8") +"&sensor=false&key=AIzaSyCvGtqoDK_SoBWG94CKOjymnOc-dzXr8WA&language=en-GB&components=country:gb");
            client.get("https://maps.googleapis.com/maps/api/place/autocomplete/json?input="+ URLEncoder.encode(selectionArgs[0].trim(), "UTF-8") +"&sensor=false&key=AIzaSyCvGtqoDK_SoBWG94CKOjymnOc-dzXr8WA&language=en-GB&components=country:gb", new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(String response) {
                    JSONObject jObject = null;
                    try {
                        jObject = new JSONObject(response);

                        JSONArray predictions = jObject.getJSONArray("predictions");
                        for(int i = 0;i < predictions.length(); i++)
                        {
                            JSONObject prediction = predictions.getJSONObject(i);
                            LocationsSuggestionProvider.this.cursor.addRow(new Object[] {i,prediction.getString("description").toString()});
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
    MatrixCursor returnMatrix = cursor;
    cursor = new MatrixCursor(COLUMNS);
    return returnMatrix;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
        String[] selectionArgs) {
    return 0;
}

}

推荐答案

原来,你不需要做任何的这个异步因为内容提供商的请求没有在UI线程上运行反正。这意味着你可以做web请求通常没有一个异步任务,或任何你会做,它会正常工作。

It turns out that you don't need to do any of this asynchronously because the request to the content provider isn't ran on the UI thread anyway. This means you can just do the web request normally without an async task or whatever you would do and it will work fine.