我有一个运行MySQL数据库的服务器,我有一个php文件来获取用户的所有任务(我必须通过POST给它的用户名),然后它返回一个JSON与查询的结果。
在我的应用程序中,我使用Apache HttpClient库来制作连接,但我似乎无法找到如何将我想要的用户名传递给php文件。
<?php
$encargado = $_POST['encargado'];
$host_name = "falseHost";
$database = "db";
$user_name = "merlin";
$password = "123";
$connect = mysqli_connect($host_name, $user_name, $password, $database);
$select = "SELECT * from tasker WHERE encargado ='$encargado' AND visible = 1 ORDER BY fecha";
$result = $connect->query($select);
$rows = array();
while($r = mysqli_fetch_assoc($result)) {
$rows[] = $r;
}
echo json_encode($rows);
$connect->close();
?>
在我的Java类中,我成功地检索了一个简单查询的结果(不需要任何POST参数)
public class Conector {
static CloseableHttpClient httpclient = HttpClients.createDefault();
//Devuelve un ArrayList con todos los username registrados
public List<String> obtenerUsuarios() throws ClientProtocolException, IOException{
List<String> listUserName = new ArrayList<>();
try {
HttpGet httpGet = new HttpGet("https://falsa.com/aplicaciones/f/select_users.php");
CloseableHttpResponse response1 = httpclient.execute(httpGet);
try {
HttpEntity entity1 = response1.getEntity();
String json = IOUtils.toString(entity1.getContent(), "UTF8");
Gson gson = new Gson(); // Or use new GsonBuilder().create();
Type collectionType = new TypeToken<Collection<User>>(){}.getType();
List<User> usuarios = (List<User>) new Gson()
.fromJson( json , collectionType);
for(User user: usuarios) {
listUserName.add(user.getUsername());
}
EntityUtils.consume(entity1);
} finally {
response1.close();
}
} finally {
httpclient.close();
}
return listUserName;
}
public List<Task> obtenerTareas(){
List<Task> obtenerTareas = new ArrayList<>();
try {
HttpPost httpPost = new HttpPost("https://falsa.com/aplicaciones/f/select_tasks.php");
CloseableHttpResponse response1 = httpclient.execute(httpPost);
try {
HttpEntity entity1 = response1.getEntity();
String json = IOUtils.toString(entity1.getContent(), "UTF8");
Gson gson = new Gson(); // Or use new GsonBuilder().create();
Type collectionType = new TypeToken<Collection<Task>>(){}.getType();
List<Task> usuarios = (List<Task>) new Gson()
.fromJson( json , collectionType);
for(User user: usuarios) {
obtenerTareas .add(user.getUsername());
}
EntityUtils.consume(entity1);
} finally {
response1.close();
}
} finally {
httpclient.close();
}
return null;
}
}