Android的 - 连接到MySQL数据库 [英] Android - Connecting to MySQL database
问题描述
我一直在尝试上连接一个MySQL数据库到Android不同的网站显示的教程。目前的code以下,取值您辛苦code的(即年= 2005和名称=汤),并通过PHP脚本运行它。我想知道的是我怎么能修改此,因此,它需要用户输入的文本框,而不是什么值。
com.test包;
进口java.io.BufferedReader中;
进口的java.io.InputStream;
进口java.io.InputStreamReader中;
进口的java.util.ArrayList;
进口org.apache.http.HttpEntity;
进口org.apache.http.Htt presponse;
进口org.apache.http.NameValuePair;
进口org.apache.http.client.HttpClient;
进口org.apache.http.client.entity.UrlEn codedFormEntity;
进口org.apache.http.client.methods.HttpPost;
进口org.apache.http.impl.client.DefaultHttpClient;
进口org.apache.http.message.BasicNameValuePair;
进口org.json.JSONArray;
进口org.json.JSONException;
进口org.json.JSONObject;
进口android.app.Activity;
进口android.os.Bundle;
进口android.util.Log;
进口android.widget.LinearLayout;
进口android.widget.TextView;
公共类测试延伸活动{
/ **第一次创建活动时调用。 * /
TextView的TXT;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
//创建一个粗观点 - 这确实应该通过布局资源设置
//但由于它的一个例子扑救声明它们的XML。
的LinearLayout rootLayout =新的LinearLayout(getApplicationContext());
TXT =新的TextView(getApplicationContext());
rootLayout.addView(TXT);
的setContentView(rootLayout);
//设置文本并调用connect函数。
txt.setText(连接...);
//调用该方法来运行数据检索
txt.setText(getServerData(KEY_121));
}
公共静态最后弦乐KEY_121 =HTTP://***.****.com/mysqlcon.php; //我用我的真实IP这里
私人字符串getServerData(字符串returnString){
InputStream的是= NULL;
字符串结果=;
//一年要发送的数据
ArrayList的<的NameValuePair> namevaluepairs中=新的ArrayList<的NameValuePair>();
nameValuePairs.add(新BasicNameValuePair(年,2005));
nameValuePairs.add(新BasicNameValuePair(名,汤姆));
// HTTP POST
尝试{
HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httppost =新HttpPost(KEY_121);
httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
HTT presponse响应= httpclient.execute(httppost);
HttpEntity实体= response.getEntity();
是= entity.getContent();
}赶上(例外五){
Log.e(log_tag,错误的HTTP连接+ e.toString());
}
//转换响应串
尝试{
的BufferedReader读卡器=新的BufferedReader(新InputStreamReader的(就是,ISO-8859-1),8);
StringBuilder的SB =新的StringBuilder();
串线= NULL;
而((行= reader.readLine())!= NULL){
sb.append(行+\ N);
}
is.close();
结果= sb.toString();
}赶上(例外五){
Log.e(log_tag,错误转换结果+ e.toString());
}
//解析JSON数据
尝试{
JSONArray jArray =新JSONArray(结果);
的for(int i = 0; I< jArray.length();我++){
的JSONObject json_data = jArray.getJSONObject(ⅰ);
Log.i(log_tag,ID:+ json_data.getInt(ID)+
,名称:+ json_data.getString(姓名)+
性别:+ json_data.getInt(性)+
,birthyear:+ json_data.getInt(birthyear)
);
//获取输出到屏幕上
returnString + =\ñ\ t+ jArray.getJSONObject(我);
}
}赶上(JSONException E){
Log.e(log_tag,错误分析数据+ e.toString());
}
返回returnString;
}
}
好吧,我知道你想要什么。你想拥有的应用程序,一个EditText或两个。用户将pssing按钮日在EditText上的信息应该移动到mysql数据库中,并$ P $后输入信息。所以,你想一个动态的应用程序,插入记录到MySQL。对于做以下修改上面的Java code。
下面插入下面的行, TextView的TXT;
的EditText editTxt;
在 rootLayout.addView(TXT); 插入以下两行,
editTxt =新的EditText(本);
rootLayout.addView(editTxt);
现在,修改此行, nameValuePairs.add(新BasicNameValuePair(名,汤姆)); 从EditText上取值
nameValuePairs.add(新BasicNameValuePair(名,editTxt.getText()的toString()));
您也可以创建一个为一年。
I have been trying out the tutorials shown on various websites on connecting a MySQL database to android. At the moment the code below, takes the values which you hard code in, (i.e year=2005 and name=tom) and runs it through the php script. What I would like know is how I can modify this, so that it takes the values of what the user enters in a textbox instead.
package com.test;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;
public class test extends Activity {
/** Called when the activity is first created. */
TextView txt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create a crude view - this should really be set via the layout resources
// but since its an example saves declaring them in the XML.
LinearLayout rootLayout = new LinearLayout(getApplicationContext());
txt = new TextView(getApplicationContext());
rootLayout.addView(txt);
setContentView(rootLayout);
// Set the text and call the connect function.
txt.setText("Connecting...");
//call the method to run the data retrieval
txt.setText(getServerData(KEY_121));
}
public static final String KEY_121 = "http://***.****.com/mysqlcon.php"; //i use my real ip here
private String getServerData(String returnString) {
InputStream is = null;
String result = "";
//the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("year","2005"));
nameValuePairs.add(new BasicNameValuePair("name","tom"));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(KEY_121);
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());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
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());
}
//parse json data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","id: "+json_data.getInt("id")+
", name: "+json_data.getString("name")+
", sex: "+json_data.getInt("sex")+
", birthyear: "+json_data.getInt("birthyear")
);
//Get an output to the screen
returnString += "\n\t" + jArray.getJSONObject(i);
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return returnString;
}
}
Okay, I understand what do you want. You want to have an EditText or two in the app. The user will enter information in it and after pressing a button th information in the EditText should move into the mysql database. So, you want to make a dynamic app that inserts record into mysql. For that do the following to modify the above java code.
Insert following line below, TextView txt;
EditText editTxt;
Below rootLayout.addView(txt); insert following two lines,
editTxt = new EditText(this);
rootLayout.addView(editTxt);
Now, modify this line, nameValuePairs.add(new BasicNameValuePair("name","tom")); to fetch value from the EditText.
nameValuePairs.add(new BasicNameValuePair("name",editTxt.getText().toString()));
You can also create one for year.
这篇关于Android的 - 连接到MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!