Witam.
Mam problem z aktualizowaniem wpisów do bazy danych za pośrednictwem skryptu PHP na serwerze.
kody PHP (Hasło i login podaje bo na tej bazie i tak nic nie ma specjalnego):
<?php
define('DB_USER', "u862287177_stach");
define('DB_PASSWORD', "password123");
define('DB_DATABASE', "u862287177_tests");
define('DB_SERVER', "mysql.hostinger.pl");
?>
<?php
class DB_CONNECT {
function __construct() {
$this->connect();
}
function __destruct() {
$this->close();
}
function connect() {
require_once __DIR__ . '/db_config.php';
$con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());
$db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());
return $con;
}
function close() {
mysql_close();
}
}
?>
<?php
$response = array();
if (isset($_POST['id']) && isset($_POST['user']) && isset($_POST['pass']) && isset($_POST['table'])) {
$id = $_POST['id'];
$user = $_POST['user'];
$pass = $_POST['pass'];
$table = $_POST['table'];
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
$result = mysql_query("UPDATE '$table' SET user = '$user', pass = '$pass' WHERE id = '$id");
if ($result) {
$response["success"] = 1;
$response["message"] = "User successfully updated.";
echo json_encode($response);
} else {
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
Pliki Java
class SaveProductDetails extends AsyncTask<String, String, String> {
@Override
private static final String url_update_product = "http://stasbar.esy.es/update_product.php";
protected String doInBackground(String... args) {
// getting updated data from EditTexts
String user = etUser.getText().toString();
String pass = etPass.getText().toString();
Log.d("recordId + user + pass+ TABLE_NAME",recordId+ user+pass+TABLE_NAME);
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", recordId));
params.add(new BasicNameValuePair("user", user));
params.add(new BasicNameValuePair("pass", pass));
params.add(new BasicNameValuePair("table", TABLE_NAME));
// sending modified data through http request
// Notice that update product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_update_product,
"POST", params);
// check json success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully updated
dismiss();
} else {
// failed to update product
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "utf-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
Log.d("reader.readLine()",line);
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
Logcat pokazuje:
D/recordId + user + pass+ TABLE_NAME﹕ 25 ggg 55885 tabela1
E/JSON Parser﹕ Error parsing data org.json.JSONException: End of input at character 0 of
Czyli nie ma odpowiedźi na mojego POSTa bo nawet jak ręcznie wejde na ten adres http://stasbar.esy.es/update_product.php to dostaje odpowiedź. Macie jakieś pomysły ?
Dodam że gdy wysyłam zapytanie bez parametrów to dostaje zwrotne D/reader.readLine()﹕ {"success":0,"message":"Required field(s) is missing"}