What is the problem with the code
if data read from my sdcard informat of "some big or small string" then its work fine and show "successfully Entered" but when its with apostrophe like "some big or small string's" then its not work and show "oops! Please try again!" from my song.php file.
This one is my song.php code
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$date_today = $_POST['date_today'];
$data_name = $_POST['data_name'];
$data_value = $_POST['data_value'];
require_once('Connect.php');
$sql = "INSERT INTO songs VALUES('$data_name','$data_value','$date_today')";
if(mysqli_query($con,$sql)){
echo 'successfully Entered';
}else{
echo 'oops! Please try again!';
}
mysqli_close($con);
}else{
echo 'error';
}
Connect.php
<?php
define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','dracula');
$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
?>
SongData class
class SongData extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... params) {
ContentResolver mr = getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor mc = mr.query(uri,null,null,null,null);
if (mc!=null&&mc.moveToFirst()){
int titleColumn = mc.getColumnIndex(MediaStore.Audio.Media.TITLE);
int idColumn = mc.getColumnIndex(MediaStore.Audio.Media._ID);
int artist = mc.getColumnIndex(MediaStore.Audio.Media.ARTIST);
do{
long thisId = mc.getLong(idColumn);
String thisTitle = mc.getString(titleColumn);
String thisArtist = mc.getString(artist);
String mp = "TITLE "+thisTitle+"\nARTIST "+thisArtist;
song += "\n\n-------------------------\n"+mp;
}while(mc.moveToNext());
mc.close();
}
return song;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
register(date_today, device, s, "http://192.168.1.102/song.php");
}
}
here is register method.
private void register(String data_name, String data_value,String date_current,String url) {
class RegisterUser extends AsyncTask<String, Void, String>{
ConnectionClass cc = new ConnectionClass();
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
}
@Override
protected String doInBackground(String... params) {
HashMap<String, String> data = new HashMap<>();
data.put("data_name",params[0]);
data.put("data_value", params[1]);
data.put("date_today", params[2]);
String result = cc.sendPostRequest(params[3],data);
return result;
}
}
RegisterUser ru = new RegisterUser();
ru.execute(data_name,data_value,date_current,url);
}
Here is ConnectionClass.java
import android.util.Log;
import org.apache.http.HttpException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
public class ConnectionClass {
public String sendPostRequest(String requestURL,
HashMap<String, String> postDataParams) {
URL url;
String response = "";
try {
url = new URL(requestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
response = br.readLine();
}
else {
response="Error Registering";
}
} catch (Exception e) {
e.printStackTrace();
response = "not connected";
}
return response;
}
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for(Map.Entry<String, String> entry : params.entrySet()){
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
Log.i("result",result.toString());
return result.toString();
}
}
I got answer. i edit and put below line in my SongData Class onPostExecute
mehod
String newStr = s.replace("\'", "");
register(date_today, device, newStr, "http://192.168.1.102/song.php");