我对使用查询字符串发送HTTP Post感到怀疑。
我有下面的代码,但是代码不能正常工作。我尝试通过Web服务发送用户和密码,嵌入在URL中,但它无法正常工作。此代码无法在Web服务上连接。
@Override
protected String doInBackground(String... params) {
String result = "";
try {
URL url = new URL("http://192.168.0.11:8080/api/Usuario/doLogin?user="+user+"&senha="+password);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = bufferedReader.readLine()) != null) {
response.append(inputLine);
}
result = response.toString();
bufferedReader.close();
} catch (Exception e) {
Log.d("InputStream", e.getMessage());
}
return result;
}
答案 0 :(得分:0)
我认为你的意思是GET请求不是POST, 你应该在查询参数中对变量进行编码," user"和#34;密码"在你的情况下。
URL url = new URL("http://192.168.0.11:8080/api/Usuario/doLogin?user=" + URLEncoder.encode(user, "UTF-8")+"&senha="+ URLEncoder.encode(password, "UTF-8"));