如何获取此请求的完整网址?我只需要 HttpGet get = new HttpGet(authUrlTemplate);
BasicHttpParams bhp = new BasicHttpParams();
bhp.setParameter("client_id", getClientId());
bhp.setParameter("client_secret", getClientSecret());
bhp.setParameter("redirect_uri", redirectURL.toString());
bhp.setParameter("grant_type", "authorization_code");
get.setParams(bhp);
System.err.println(get.getURI());
System.err.println(get.getRequestLine());
System.err.println(get.getParams());
。我如何构建URL?
没有任何print语句打印出我想要的内容,getURI()不会添加参数......
21:18:49,967 ERROR [stderr] (default task-7) https://accounts.google.com/o/oauth2/v2/auth
21:18:49,970 ERROR [stderr] (default task-7) GET https://accounts.google.com/o/oauth2/v2/auth HTTP/1.1
21:18:49,971 ERROR [stderr] (default task-7) org.apache.http.params.BasicHttpParams@28310ff2
输出:
LayoutParams params = holder.itemView.getLayoutParams();
params.height = screenWidth/2;
params.width = screenHeight*24/100;
holder.itemView.setLayoutParams(params);
我知道有一个库可以做到这一点,但是想这样做。
答案 0 :(得分:1)
这是一个通用的工作示例。代码很冗长,可以缩短,但这给你一个整体的想法。
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import sun.management.resources.agent;
import java.util.*;
/**
* Created by aditya on 9/24/2016.
*/
public class SOMain {
static public void main(String[] args){
String baseUrl="http://google.co.in";
HashMap<String,String> parameters = new HashMap<String,String>();
parameters.put("key1","value1");
parameters.put("key3","value3");
parameters.put("key2","value2");
System.out.println(addParameters(baseUrl,parameters));
}
static protected String addParameters(String url, HashMap<String,String> parameters){
if(!url.endsWith("?"))
url += "?";
List<NameValuePair> params = new LinkedList<NameValuePair>();
if(parameters!=null){
Iterator entries = parameters.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next();
String key = (String)entry.getKey();
String value = (String)entry.getValue();
params.add(new BasicNameValuePair(key, value)); }
}
String paramString = URLEncodedUtils.format(params, "utf-8");
url += paramString;
return url;
}
}
打印
http://google.co.in?key1=value1&key2=value2&key3=value3
答案 1 :(得分:0)
以下是我如何做到这一点的简单版本:
public String getAuthUrl() {
URIBuilder b = null;
// Removed exception handling...
b = new URIBuilder(AUTH_URL); // http://whatever.com
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("client_id", getClientId()));
nvps.add(new BasicNameValuePair("redirect_uri", redirectURL.toString()));
nvps.add(new BasicNameValuePair("state", STATE));
nvps.add(new BasicNameValuePair("response_type", "code"));
nvps.add(new BasicNameValuePair("scope", "email"));
b.setParameters(nvps);
return b.toString();
}