I want to send data to the database.My PHP Files are OK,because I had tested that with postman and it send data to the database.But when I used Android app for that it shouldn't send data to the database.Here is my java method for the sending data.Reply me with the solution please.
public void insertData(String name,String category,int quantity){
String url = "http://192.168.172.2/foods/insert.php";
try {
String data = URLEncoder.encode("name", "UTF-8")
+ "=" + URLEncoder.encode(name, "UTF-8");
data += "&" + URLEncoder.encode("category", "UTF-8") + "="
+ URLEncoder.encode(category, "UTF-8");
data += "&" + URLEncoder.encode("quantity", "UTF-8")
+ "=" + URLEncoder.encode(String.valueOf(quantity), "UTF-8");
URL url1 = new URL(url);
HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(data);
writer.flush();
Log.d("datainsert","success"+data);
} catch (MalformedURLException e) {
Log.d("datainsert","Error"+e.getMessage());
} catch (IOException e) {
Log.d("datainsert","Error"+e.getMessage());
}
<?PHP
require "config.php";
global $connect;
if (isset($_POST['name']) and isset($_POST['category']) and isset($_POST['quantity'])) {
$name = $_POST['name'];
$category = $_POST['category'];
$quantity = $_POST['quantity'];
$intq = (int)$quantity;
if ($_SERVER['REQUEST_METHOD']== "POST") {
$query = "INSERT INTO food_details(food_name,category,quantity) VALUES('$name','$category',$intq);";
if($q = mysqli_query($connect,$query)){
echo "successfully inserted";
}else{
//echo mysqli_error(mysqli_query($connect,$query));
echo "not success";
echo "Error: " . $query . "<br>" . $connect->error;
}
}
}
mysqli_close($connect);
?>
Sending Data to server using POST method Uri.Builder
class for creating query String.As follows
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("firstParam", paramValue1)
.appendQueryParameter("secondParam", paramValue2)
String query = builder.build().getEncodedQuery();
For your question specific your post method will be as follows
try {
URL url = new URL("http://192.168.172.2/foods/insert.php");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setReadTimeout(10000); //change according to need
conn.setConnectTimeout(15000); //change according to need
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("name", name)
.appendQueryParameter("category", category)
.appendQueryParameter("quantity", quantity);
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();
//get response from server
String response = conn.getResponseMessage();
Log.d("URl->", response);
} catch (IOException e) {
e.printStackTrace();
}
I hope its work for you
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.