我需要点击具有路径参数的休息服务
网址:http://www.abcd.com/restapi/{username}/{password}
我不知道如何使用okHttp
执行此操作,谷歌也没有任何相同的示例。我怎么能这样做?
答案 0 :(得分:0)
String url = "http://www.abcd.com/restapi/{username}/{password}"
url = url.replace("{username}",parameter);
url = url.replace("{password}",parameter2);
然后继续使用网址
答案 1 :(得分:0)
///嘿首先在Url下面放一个日志,然后检查添加用户名密码后发送的URL。但在使用此
之前,仍然使用下面的代码命中服务器更改您的URL private class Async extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
//createDialog();
}
@Override
protected String doInBackground(Void... params) {
StringBuffer response = null;
try {
URL url = new URL(Api.BASE_URL + "username" + Utils.exchange + "password" + Utils.code);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
con.setRequestProperty("X-Authorization-Token", prefs1.getString("auth_token", ""));
int responseCode = con.getResponseCode();
if (responseCode == 401) {
return "401";
}
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
} catch (Exception er) {
System.out.println(TAG + er);
}
try {
return response.toString();
} catch (Exception e) {
return "";
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.e(TAG, s);
// dialog.cancel();
}
}