MySQL数据库连接 [英] MySQL db connection
问题描述
我一直在寻找我的Android模拟器和一个MySQL数据库之间的连接网络。结果
我发现,你无法通过Web服务器直接连接,但可以。 Web服务器会从我的Android处理我的请求。
我发现你好的Android 以下code,但我不明白。如果我运行在模拟器上这个code,什么都不会发生;屏幕保持黑色。
哪里 Log.i
的土地,在Android屏幕,错误日志,或其他地方?结果
有人可以帮我这个code?
包app.android.ticket;进口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;
公共类fetchData延伸活动{
/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
//调用该方法来运行数据retreival
getServerData();
}
公共静态最后弦乐KEY_121 =http://www.jorisdek.nl/android/getAllPeopleBornAfter.php;公共fetchData(){
Log.e(fetchData,初始化ServerLink);
}私人无效getServerData(){
InputStream为= NULL;
字符串结果=; //年的数据发送
ArrayList的<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;();
nameValuePairs.add(新BasicNameValuePair(年,1980)); // 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(I)
Log.i(log_tag,ID:+ json_data.getInt(ID)+
,名称:+ json_data.getString(名称)+
性别:+ json_data.getInt(性)+
birthyear:+ json_data.getInt(birthyear)
);
}
}赶上(JSONException E){
Log.e(log_tag,错误分析数据+ e.toString());
} }
}
记录消息都会在日志猫。您也可以使用<一个href=\"http://www.androidpit.com/en/android/market/apps/app/com.nolanlawson.logcat/CatLog-Logcat-Reader\"相对=nofollow> LogCat中阅读器。
I have been searching the web for a connection between my Android simulator and a MySQL db.
I've found that you can't connect directly but can via a web server. The web server will handle my request from my Android.
I found the following code on Hello Android, but I don't understand. If I run this code on the simulator, nothing happens; the screen stays black.
Where does Log.i
land, in the Android screen, the error log, or somewhere else?
Can somebody help me with this code?
package app.android.ticket;
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;
public class fetchData extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//call the method to run the data retreival
getServerData();
}
public static final String KEY_121 = "http://www.jorisdek.nl/android/getAllPeopleBornAfter.php";
public fetchData() {
Log.e("fetchData", "Initialized ServerLink ");
}
private void getServerData() {
InputStream is = null;
String result = "";
//the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("year","1980"));
//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")
);
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
}
Logging messages go in the Log cat. You could also use LogCat Reader.
这篇关于MySQL数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!