我的代码如下。我用httpPost写它,但我必须将它转换为httpGet。因为我们使用的webservice需要HttpGet。 我怎样才能转换它并正确使用它。 提前谢谢。
String nameValue = new String();
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 6000;
HttpConnectionParams.setConnectionTimeout(httpParameters,
timeoutConnection);
int timeoutSocket = 6000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
HttpClient httpclient = new DefaultHttpClient();
httpPost httpget = new httpPost(
"http://178.16.1.1:8080/Pets/REST/WebService/setUser");
HttpResponse response;
String result = null;
try {
HttpContext ctx = new BasicHttpContext();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
2);
nameValuePairs.add(new BasicNameValuePair("username", username
.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("password", pasword
.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("email", username
.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("city", pasword
.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("county", username
.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("adress", pasword
.getText().toString()));
httpget.setEntity(new UrlEncodedFormEntity(nameValuePairs,
"UTF-8"));
response = httpclient.execute(httpget, ctx);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity);
JSONObject arr = new JSONObject(result);
nameValue = (arr.get("error")).toString();
}
return nameValue;
答案 0 :(得分:1)
GET按照here
所述在网址http://178.16.1.1:8080/Pets/REST/WebService/setUser?username=username&password=password
中发送参数
所以你应该像这样使用StringBuilder
:
String[] parameters = new String[]{"username", "password"};
String[] values = new String[]{"username", "password"};
StringBuilder url = new StringBuilder(
"http://178.16.1.1:8080/Pets/REST/WebService/setUser");
url.append('?');
for (int i = 0; i < parameters.length; i++) {
url.append(parameters[i]+"="+values[i]+"&");
}
url.deleteCharAt(url.length());
HttpGet httpget = new HttpGet(url.toString());
这将为HTTP GET
URL
次来电
http://178.16.1.1:8080/Pets/REST/WebService/setUser?username=username&password=password