【发布时间】:2012-03-06 15:32:32
【问题描述】:
我已经创建了我的数据库 MySQL,我希望将数据从 android 应用程序发送到数据库中,但我不能这样做。我正在使用 wamp 服务器。
这是我的 php 代码:
<?php mysql_connect("localhost","cupcake","123456");
mysql_select_db("cindy");
$sql=mysql_query("insert into yang (name)values('".$_REQUEST['i_name']."')");
$r=mysql_query($sql);
if(!$r)echo "Error in query: ".mysql_error();mysql_close();?>
这是我来自 Android 的 Java 文件
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String result = null;
JSONArray jArray;
InputStream is = null;
StringBuilder sb=null;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("i_name","aaa"));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/testing.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection"+e.toString());
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n"); }
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}//paring datatry
try {
jArray = new JSONArray(result);
//int[] deal_id=new int[jArray.length()+1];
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
}
} catch(JSONException e1){
} catch (ParseException e1) {
e1.printStackTrace(); }
}}
这是我创建的 MySQL 表
--
CREATE TABLE IF NOT EXISTS `yang` (
`name` varchar(100) NOT NULL,
PRIMARY KEY (`name`)
)
请帮帮我..谢谢...
【问题讨论】:
-
你得到了什么?有什么错误吗?
-
@Thilo,我没有任何错误...我的代码可以工作,但我的表中没有数据..
标签: java android mysql database phpmyadmin