gpt4 book ai didi

android - API 请求通过本地主机工作但不通过网络服务器工作

转载 作者:行者123 更新时间:2023-11-30 04:59:13 27 4
gpt4 key购买 nike

我正在尝试将 json 数据提取到我的 android 应用程序中。当我使用本地服务器时它工作正常,但是当我将它迁移到 Web 服务器时它不工作。我检查了浏览器中的 api 链接,它们在浏览器中提供了数据

这是我在错误日志中得到的:

E/com.newrelic.android: TransactionStateUtil: Attempting to convert network exception java.net.SocketTimeoutException to error code.
E/com.newrelic.android: HttpsURLConnectionExtension: java.io.IOException: CountingInputStream: input stream cannot be null
E/final123: http://shopcite.rf.gd/shopcite/fetch_hotels.php?city=mathura&origin=18.5914802,73.7075401

最后一行是获取数据的那一行。我正在通过 AsyncTask 在 android 中获取结果,因此在主线程上没有完成任何工作。

public class hotelList extends AsyncTask<String, Integer, List<HotelModel>>{
@Override
protected List<HotelModel> doInBackground(String... strings) {
List<HotelModel> hotelModelList = new ArrayList<>();
if (!dataSet) {
HttpHandler sh = new HttpHandler();
jsonStr = sh.makeServiceCall(HotelURL + CONSTANTS.city+"&origin=" + CONSTANTS.origin);
Log.e("final123", HotelURL + CONSTANTS.city+"&origin=" + CONSTANTS.origin);
dataSet = true;
if (jsonStr != null) {
try {
JSONArray listArray = new JSONArray(jsonStr);
for (int i = 0; i < listArray.length(); i++) {
JSONObject hotelObject = listArray.getJSONObject(i);
HotelModel hotelModel = new HotelModel();
hotelModel.setName(hotelObject.getString("name"));
hotelModel.setAddr(hotelObject.getString("addr"));
hotelModel.setCateg(hotelObject.getString("categ"));
hotelModel.setImg(CONSTANTS.server_domain+hotelObject.getString("image"));
hotelModel.setPrice(hotelObject.getInt("price"));
hotelModel.setRating((float) hotelObject.getDouble("rating"));
hotelModel.setHotel_id(hotelObject.getInt("hotel_id"));
hotelModel.setDist(hotelObject.getInt("dist"));
hotelModelList.add(hotelModel);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}else {
if (jsonStr != null) {
try {
JSONArray listArray = new JSONArray(jsonStr);
for (int i = 0; i < listArray.length(); i++) {
JSONObject hotelObject = listArray.getJSONObject(i);
HotelModel hotelModel = new HotelModel();
hotelModel.setName(hotelObject.getString("name"));
hotelModel.setAddr(hotelObject.getString("addr"));
hotelModel.setCateg(hotelObject.getString("categ"));
hotelModel.setImg(CONSTANTS.server_domain+hotelObject.getString("image"));
hotelModel.setPrice(hotelObject.getInt("price"));
hotelModel.setRating((float) hotelObject.getDouble("rating"));
hotelModel.setHotel_id(hotelObject.getInt("hotel_id"));
hotelModel.setDist(hotelObject.getInt("dist"));
hotelModelList.add(hotelModel);
}
} catch (JSONException e) {
e.printStackTrace();
}if (orderby.equals("location")) {
Log.e("ll", String.valueOf(hotelModelList.get(0).getDist()));
Collections.sort(hotelModelList, new Comparator<HotelModel>() {
@Override
public int compare(HotelModel o1, HotelModel o2) {
return o1.getDist() - o2.getDist();
}
});
}
else if (orderby.equals("price")) {
Collections.sort(hotelModelList, new Comparator<HotelModel>() {
@Override
public int compare(HotelModel o1, HotelModel o2) {
return o1.getPrice() - o2.getPrice();
}
});
} else if (orderby.equals("rating")) {
Collections.sort(hotelModelList, new Comparator<HotelModel>() {
@Override
public int compare(HotelModel o1, HotelModel o2) {
return (int) (o2.getRating() * 100 - o1.getRating() * 100);
}
});
}
}
}
return hotelModelList;
}
@Override
protected void onPostExecute(List<HotelModel> hotelModels) {
super.onPostExecute(hotelModels);
HotelAdapter hotelAdapter = new HotelAdapter(hotel_populate_list.this,R.layout.item_hotel,hotelModels);
hotelPopulate.setAdapter(hotelAdapter);
progressDialog.dismiss();
}

@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog=new ProgressDialog(hotel_populate_list.this);
progressDialog.setIndeterminate(true);
progressDialog.setCancelable(false);
progressDialog.setMessage("Gathering details from Internet...");
progressDialog.show();
}
}

应用程序没有崩溃,但我看不到获取的结果。请注意,当我通过本地主机执行此操作时,它正在获取结果

我试图将以下内容放入 Log.e 中以查看是否正在获取 json 数据。它没有获取,它给了我空指针异常

jsonStr = sh.makeServiceCall(HotelURL + CONSTANTS.city+"&origin=" + CONSTANTS.origin);

添加 makeServiceCall 方法

public String makeServiceCall(String reqUrl) {
String response = null;
try {
URL url = new URL(reqUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");

// read the response
InputStream in = new BufferedInputStream(conn.getInputStream());
response = convertStreamToString(in);
} catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException: " + e.getMessage());
} catch (ProtocolException e) {
Log.e(TAG, "ProtocolException: " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "IOException: " + e.getMessage());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
return response;
}

private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();

String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}

更新:

我进一步检查,这与虚拟主机阻止来自移动/安卓的流量有关。我尝试了多个主机,不幸的是这对所有人都是一样的。有没有办法让这些调用看起来像是来自浏览器?

最佳答案

服务器连接使用http协议(protocol)。您将需要设置 https,或者如果您需要它是 http,并且您的应用程序的目标是 Api 级别 28 或更高级别,您需要在您的 list 中进行设置:

<application
...
android:usesCleartextTraffic="true"
...>

如果不行,问题出在服务器端

关于android - API 请求通过本地主机工作但不通过网络服务器工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58529648/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com