简体   繁体   English

构建一个JSON搜索解析器

[英]Building a JSON search parser

I am working with a database api and they give me the opportunity to search using a url through their database. 我正在使用数据库api,他们给了我机会通过他们的数据库使用url进行搜索。

This is the url = 这是网址=

http://api.database.com/v2/search?q=(THE PRODCUT)&type=(THE TYPE OF PRODUCT)
&key=(myApiKey)

I want to make a simple search bar were the user can type the product name and choose a type (catagorie) to insert that into the url and then look the product up in the database. 我想做一个简单的搜索栏,用户可以输入产品名称,然后选择一种类型(目录)以将其插入url,然后在数据库中查找产品。

I know how I can parse the data from the url but how can I insert the users text into the url to change it ? 我知道如何解析url中的数据,但是如何将用户文本插入url中进行更改?

You can use string concatenation or you can use String.format(String format, Object... args). 您可以使用字符串连接,也可以使用String.format(String format,Object ... args)。 For example: 例如:

String url = String.format("http://api.database.com/v2/search?q=%s&type=%s&key=%", product, productType, apiKey);

or : 要么 :

var product = "Product";
var productType="ProductType";
var url = "http://api.database.com/v2/search?q="+product+"&type="+productType+"&key="+myApiKey;

It's always a better approach to use URI to build your search url. 使用URI来构建搜索URL始终是一种更好的方法。

Try something like this 试试这个

final String FORECAST_BASE_URL ="http://api.openweathermap.org/data/2.5/forecast/daily?";
                final String QUERY_PARAM = "q";
Uri builtUri = Uri.parse(FORECAST_BASE_URL).buildUpon()
                        .appendQueryParameter(QUERY_PARAM, params[0])
                        .build();
URL url = new URL(builtUri.toString());
                urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.connect();

And then read the response from the inputStream. 然后从inputStream读取响应。

 InputStream inputStream = urlConnection.getInputStream();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM