HttpClient
或HttpPost
等命令无效。谁能帮助我?
@Override
protected users doInBackground(Void... params){
Map<String, String> dataToSend = new HashMap<>();
dataToSend.put("x", users.x + "");
dataToSend.put("y", users.y);
URL url = new URL(SERVER_ADRESS);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
HttpParams httpRequestPramas = new BasicHttpParams();
conn.setConnectionTimeout(httpRequestPramas, CONNECTION_TIME);
HttpConnectionParams.setSoTimeout(httpRequestPramas, CONNECTION_TIME);
HttpClient client = new DefaultHttpClient(httpRequestPramas);
HttpPost post = new HttpPost(SERVER_ADRESS + "login.php");
users returnedusers = null;
try {
post.setEntity(new UrlEncodedFormEntity(dataToSend));
HttpResponse httpResponse = client.execute(post);
HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
JSONObject jObject = new JSONObject(result);
if (jObject.length() == 0){
users = null;
}else{
String vorname = jObject.getString("y");
int kundennummer = jObject.getInt("x");
returnedusers = new users(users.y, users.x);
}
}catch (Exception e){
e.printStackTrace();
}
return returnedusers;
}
感谢您的帮助。
答案 0 :(得分:3)
试试这种方式
URL url = new URL("http://example.sitedemo.service.php");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
Uri.Builder builder = new Uri.Builder().appendQueryParameter("username", "maven")
.appendQueryParameter("password", "123");
String query = builder.build().getEncodedQuery();
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
conn.connect();
InputStream in = new BufferedInputStream(conn.getInputStream());
response = IOUtils.toString(in, "UTF-8");