我正在尝试创建一个使用 WAMP 服务器将数据发送到远程数据库的应用程序。我面临的问题是没有人出现:没有发送数据,也没有显示 logcat 错误,我也无法插入 Toast 以查看应用程序是否进入代码的特定部分,因为 Eclipse 在运行时将其显示为错误模式。
所以,这里是 .java 代码:
public class AggiungiProdotto extends Activity
{
private static String indirizzo ="http://10.0.2.2/tesina/Aggiungi_Ordinazione";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.aggiungi_prodotto);
new AggiungoOrdinazione().execute();
}
private class AggiungoOrdinazione extends AsyncTask<String, Void, String>
{
@Override
protected void onPreExecute()
{
super.onPreExecute();
}
@Override
protected String doInBackground(String... arg0)
{
InputStream is = null;
String result = "";
JSONObject jsonResult = null;
TextView tv;
Intent intent = getIntent();
String Nome = new String();
String Tavolo = intent.getStringExtra("Tavolo");
Nome = "ciao";
HttpPost httppost = new HttpPost(indirizzo);
HttpParams httpParams = new BasicHttpParams();
int timeOutConnection = 5000;
HttpConnectionParams.setConnectionTimeout(httpParams, timeOutConnection);
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParams, timeoutSocket);
HttpClient httpclient = new DefaultHttpClient(httpParams);
List<NameValuePair> Comanda = new ArrayList<NameValuePair>();
Comanda.add(new BasicNameValuePair("Nome", Nome ));
Comanda.add(new BasicNameValuePair("Tavolo", Tavolo));
try
{
httppost.setEntity(new UrlEncodedFormEntity(Comanda));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
jsonResult = new JSONObject(result);
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (JSONException e)
{
e.printStackTrace();
}
return null;
}
}
protected void onPostExecute()
{
}
}
然后这里是与将数据插入数据库相关的 .PHP 代码:
<?php
// Dichiaro una array nel quale inserirò la risposta in JSON
$response = array();
/* La funzione isset controlla se all'interno della variabile esiste un dato diverso da null.
$_POST è una funzione che inserisce i dati passati attraverso un metodo POST alla variabile seguente. */
if (isset($_POST['Nome']) && isset($_POST['Tavolo']))
{
$Nome = $_POST['Nome'];
$Tavolo = $_POST['Tavolo'];
// Includo la classe per la connessione al database
require_once __DIR__ . '/connessione_db.php';
// Da file incluso posso istanziare un oggetto della clase
$db = new DB_CONNECT();
// Query in mySQL Per creare una riga i cui campi hanno i valori passati dall'applicazione.
$result = mysql_query("INSERT INTO pizze(Nome, Tavolo) VALUES('$Nome', '$Tavolo')");
// Controllo se la riga è stata inserita oppure no.
if ($result)
{
$response["Stato"] = 1;
$response["Messaggio"] = "Dati inseriti!";
// echo in PHP che mi converte il risultato in JSON e la mette nella variabile response.
echo json_encode($response);
}
else
{
$response["success"] = 0;
$response["message"] = "Dati non inseriti!";
// Converto anche qui.
echo json_encode($response);
}
} else
{
$response["success"] = 0;
$response["message"] = "Campo richiesto mancante!";
// Converto.
echo json_encode($response);
}
?>
你能帮我出什么问题吗?我做的方式是正确的还是我错过了什么?谢谢你。