对于此分配,我们应该对URL进行编码。最终,输出应该是 http://www.amazon.com?id=123&author=Jim+Campbell&publisher=O%27Reilly,但我得到www.amazon.com?id=123?author=Jim+Campbell?publisher=O%27Reilly
现在这里是我的代码:
package MyUrl;
import java.util.Scanner;
public class MyUrl {
private String mUrl;
public MyUrl(String url) {
if (url.contains("http://"))
mUrl = "http://" + url;
else
mUrl = url;
}
public void addArgument(String name, String value) {
if (mUrl.indexOf(name) == '?') {
mUrl = mUrl + '&' + urlEncode(name) + '=' + urlEncode(value);
} else {
mUrl = mUrl + '?' + urlEncode(name) + '=' + urlEncode(value);
}
}
public void addArgument(String name, int ivalue) {
String newValue = Integer.toString(ivalue);
if (mUrl.indexOf(name) == '?') {
mUrl = mUrl + '&' + urlEncode(name) + '=' + urlEncode(newValue);
} else {
mUrl = mUrl + '?' + urlEncode(name) + '=' + urlEncode(newValue);
}
}
public void addArgument(String name, double dvalue) {
String newValue1 = Double.toString(dvalue);
if (mUrl.indexOf(name) == '?') {
mUrl = mUrl + '&' + urlEncode(name) + '=' + urlEncode(newValue1);
} else {
mUrl = mUrl + '?' + urlEncode(name) + '=' + urlEncode(newValue1);
}
}
public String toString() {
String result = mUrl;
return result;
}
public static String urlEncode(String text) {
String newWord = "";
for (int n = 0; n < text.length(); ++n) {
char c = text.charAt(n);
String hexValue = Integer.toHexString(c);
if (c >= 'A' && c <= 'Z') {
newWord += c;
} else if (c >= 'a' && c <= 'z') {
newWord += c;
} else if (c >= '0' && c <= '9') {
newWord += c;
} else if (c == '_' || c == '-' || c == '.' || c == '*') {
newWord += c;
} else if (c == ' ') {
newWord += '+';
} else {
newWord += '%' + hexValue;
}
}
text = newWord;
return text;
}
}
/**
* main
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Loop, asking for a new URL to be entered.
do {
System.out.println();
System.out.println("Enter URL site (or 'exit')...");
String baseUrl = sc.nextLine();
if (baseUrl.equalsIgnoreCase("exit"))
break;
// Create a new MyUrl object and call its constructor
MyUrl u = new MyUrl(baseUrl);
System.out.println("Url value read was: " + baseUrl);
// Loop, asking for argument/value input
do {
System.out.println("Enter URL argument name (or 'done')...");
String argName = sc.nextLine();
if (argName.equalsIgnoreCase("done"))
break;
System.out.println("Enter type of argument value (string, integer, double)...");
String argType = sc.nextLine();
if (argType.startsWith("s")) {
System.out.println("Enter a string value");
String s = sc.nextLine();
u.addArgument(argName, s);
} else if (argType.startsWith("i")) {
System.out.println("Enter an integer value");
int i = sc.nextInt();
sc.nextLine();
u.addArgument(argName, i);
} else if (argType.startsWith("d")) {
System.out.println("Enter a double value");
double d = sc.nextDouble();
sc.nextLine();
u.addArgument(argName, d);
} else {
System.out.println("Unrecognized value type - must be (s)tring, (i)nteger, or (d)ouble");
continue;
}
} while (true);
// Display the final url
System.out.println("URL with appended arguments is:");
System.out.println(" " + u.toString());
} while (true);
// Keep console window alive until 'enter' pressed (if needed).
System.out.println();
System.out.println("Done - press enter key to end program");
String junk = sc.nextLine();
}
}
然后对于输出,我输入所有这些,但是没有得到我想要的结果。有人可以告诉我在我的代码中我做错了什么吗?
输入网址(或&#39;退出&#39;)... www.amazon.com
读取的Url值为:www.amazon.com
输入网址参数名称(或&#39;完成&#39;)...
ID
输入参数值的类型(字符串,整数,双精度)......
整数
输入整数值
123
输入网址参数名称(或&#39;完成&#39;)...
作者
输入参数值的类型(字符串,整数,双精度)......
的字符串
输入字符串值
吉姆坎贝尔输入网址参数名称(或&#39;完成&#39;)...
出版商
输入参数值的类型(字符串,整数,双精度)......
的字符串
输入字符串值
0&#39; Reilly的
输入网址参数名称(或&#39;完成&#39;)...
完成
带有附加参数的URL是:
www.amazon.com?id=123?author=Jim+Campbell?publisher=O%27Reilly输入网址(或&#39;退出&#39;)...
答案 0 :(得分:2)
我注意到了这些。
在您的构造函数中,您忘记了!
。取代
if (url.contains("http://"))
与
if (!url.contains("http://"))
在所有addArgument
方法中,替换
if(mUrl.indexOf(name)=='?')
与
if(mUrl.indexOf('?') != -1)
<强>解释强>
您正在添加http://
(如果它已存在),而不是相反。 !
是合乎逻辑的,它反过来了。
您正在尝试在URL中查找参数的名称,然后将索引(或-1)与'?'
的数值进行比较。显然不行。