提取卷曲API在Android中Java的

我尝试使用以下cURL命令提取从EatStreet API数据:提取卷曲API在Android中Java

卷曲-X GET \ -H 'X-访问令牌:API_EXPLORER_AUTH_KEY' \ “https://api.eatstreet.com/publicapi/v1/restaurant/search?latitude=40.4310&longitude=-86.9149&method=both”

我写了到目前为止的代码如下:

import org.apache.http.auth.UsernamePasswordCredentials; 

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.impl.auth.BasicScheme;

import org.apache.http.impl.client.DefaultHttpClient;

import java.io.BufferedReader;

import java.lang.StringBuffer;

import java.net.URI;

import java.net.URISyntaxException;

public class Main {

public static void main(String[] args) {

StringBuffer stringBuffer = new StringBuffer("");

BufferedReader bufferedReader = null;

blah();

}

private static void blah() {

try {

HttpClient httpClient = new DefaultHttpClient();

HttpGet httpGet = new HttpGet();

URI uri = new URI("https://api.eatstreet.com/publicapi/v1/restaurant/search?latitude=40.4310&longitude=-86.9149&method=both");

httpGet.setURI(uri);

addHeader(httpGet);

}

catch (URISyntaxException e) {

e.printStackTrace();

}

catch (Exception e) {

e.printStackTrace();

}

}

private void addHeader(HttpGet httpGet) {

httpGet.addHeader(BasicScheme.authenticate(new UsernamePasswordCredentials("X-Access-Token",

"__API_EXPLORER_AUTH_KEY__"), null));

}

}

此代码是不是在所有我相信任何输出是由于WR ong身份验证。我需要使用Android上面提到的cURL命令来获取数据。

输出应该类似于如下(JSON格式):

{ 

"address": {

"apiKey": null,

"streetAddress": null,

"latitude": 40.431,

"longitude": -86.9149,

"city": null,

"state": null,

"zip": null,

"aptNumber": null

},

"restaurants": [

{

"apiKey": "90fd4587554469b1144247b91fbcb2f349e8e58504b5a530",

"deliveryMin": 7,

"deliveryPrice": 3,

"logoUrl": "/wp-content/uploads/new2022/20220327voicc/151153129.png",

"name": "Egyptian Cafe and Hooka Bar",

"streetAddress": "130 Northwestern Avenue",

"city": "West Lafayette",

"state": "IN",

"zip": "47906",

"foodTypes": [

"Subs & Sandwiches",

"Middle Eastern Food",

"Mediterranean Food",

"Hookah Bars",

"Healthy Food"

],

.

.

.

{

"apiKey": "90fd4587554469b1144247b91fbcb2f3720a4edae037dd96",

"deliveryMin": 7,

"deliveryPrice": 3,

"logoUrl": "/wp-content/uploads/new2022/20220327voicc/152153129.png",

"name": "Vienna Coffee House & Bakery",

"streetAddress": "208 South Street",

"city": "West Lafayette",

"state": "IN",

"zip": "47906",

"foodTypes": [

"Coffee & Tea",

"Bakery"

],

"phone": "(765) 743-4446",

"latitude": 40.4239543,

"longitude": -86.9079021,

"minFreeDelivery": 0,

"taxRate": 0.07,

"acceptsCash": false,

"acceptsCard": true,

"offersPickup": true,

"offersDelivery": true,

"isTestRestaurant": false,

"minWaitTime": 45,

"maxWaitTime": 60,

"open": true,

"url": "https://eatstreet.com/lafayette-in/restaurants/vienna-coffee-house-and-bakery",

"hours": {

"Sunday": [

"6:00 PM - 12:00 AM"

],

"Wednesday": [

"6:00 PM - 12:00 AM"

],

"Tuesday": [

"6:00 PM - 12:00 AM"

]

},

"timezone": "US/Eastern"

}

]

}

回答:

addHeader(httpGet)后,你必须用实际执行的GET操作先前定义的httpClient

HttpResponse response = httpclient.execute(httpGet); 

response.getEntity().getContent(); // do something with the response

// an example of continuing what you were trying to do

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(

response.getEntity().getContent()));

而且,您的addHeader(httpGet)需要更改为此,以传递API密钥作为标题,而不是UsernamePasswordCredentials

private static void addHeader(HttpGet httpGet) { 

httpGet.addHeader("X-Access-Token", "__API_EXPLORER_AUTH_KEY__");

}

以上是 提取卷曲API在Android中Java的 的全部内容, 来源链接: utcz.com/qa/259822.html

回到顶部