如何在url中设置变量以在Android中获取json? 在大多数教程中都是这样的方法:
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;
}
其中 requURL 是String,例如: http://192.168.1.3:5000/test 。如果我想在此URL变量中设置该怎么办?例如, http://192.168.1.3:5000/test/{id} ,其中 id 是整数。我不想使用参数,我想在路径中使用变量。有可能吗?
提前致谢。 :)