0
這裏是我從Eclipse到包含數據庫連接的PHP文件建立連接的代碼,但它將一個空字符串存儲到數據庫。任何人都可以指出我一路上可能犯的錯誤嗎?建立從Eclipse到PHP數據庫文件的連接
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent add = new Intent(addUser.this, FavUser.class);
startActivity(add);
InputStream is = null;
EditText txtSearch = (EditText) findViewById(R.id.txtSearch);
String strSearch = txtSearch.getText().toString();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair(strSearch, strSearch));
try {
// Add your data
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/connecttodb.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (IOException e) {
Log.e("Log_tag", "error in http connection" +e.toString());
}
// convert reposonse to string
你得到了什麼錯誤? – Hend 2012-01-12 03:18:45
您至少需要使用計算機的IP地址而不是'localhost'。 – jprofitt 2012-01-12 03:35:52
檢查http://fahmirahman.wordpress.com/2011/04/21/connection-between-php-server-and-android-client-using-http-and-json/ – 2012-01-12 06:21:01