[英]connect android to mysql using php
我是android的初學者。我正在嘗試將android連接到mysql。 但是應用程序無例外地關閉了。請解決我的問題:我嘗試使用此代碼將php連接到mysql。我在布局文件中使用了textview。此外,我還在manifest
添加了internet permission
。
我的代碼是:
public class AndroidConnectActivity extends Activity
{
private TextView outputStream;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_connect);
JSONArray jArray;
String result = null;
InputStream is = null;
StringBuilder sb = null;
outputStream = (TextView)findViewById(R.id.hello_world);
ArrayList<NameValuePair> namevaluepairs = new ArrayList<NameValuePair>();
try
{
//http post
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost/android/index.php");
httppost.setEntity(new UrlEncodedFormEntity(namevaluepairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
if (response.getStatusLine().getStatusCode() != 200)
{
Log.d("MyApp", "Server encountered an error");
}
}
catch(Exception e)
{
Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show();
}
//Convert response to string
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF8"));
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
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());
}
//END Convert response to string
try
{
jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++)
{
json_data = jArray.getJSONObject(i);
int id=json_data.getInt("id");
String name=json_data.getString("name");
outputStream.append(id +"" + name + "\n");
}
}
catch(JSONException e1)
{
e1.printStackTrace();
}
catch (ParseException e1)
{
e1.printStackTrace();
}
}
}
<?php
mysql_connect("localhost","root","");
mysql_select_db("android");
$sql=mysql_query("SELECT * FROM table_1 Where name like 'M%' ");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
?>
在http://localhost/android/index.php
使用IP地址代替localhost
使用eclipse android插件調試您的應用程序。 必須拋出錯誤或異常,您需要找出它是什么。
看來您在此周圍缺少大括號:
while($row=mysql_fetch_assoc($sql))
{
$output[]=$row;
print(json_encode($output));
}
您可能無法使用“ http:// localhost / ..”從仿真器連接到本地主機。請嘗試使用此“ http://10.0.2.2/ ..”。 還要在代碼之間放置一些日志 ,並嘗試發現哪一行出現錯誤。
最后,我確定了我的錯誤。下面我提到了代碼。它工作正常,它從數據庫中檢索數據。
public class AndroidConnectActivity extends Activity
{
private TextView outputStream;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_connect);
JSONArray jArray;
String result = null;
InputStream is = null;
StringBuilder sb = null;
outputStream = (TextView)findViewById(R.id.hello_world);
ArrayList<NameValuePair> namevaluepairs = new ArrayList<NameValuePair>();
try
{
//http post
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/android/index.php");
httppost.setEntity(new UrlEncodedFormEntity(namevaluepairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
if (response.getStatusLine().getStatusCode() != 200)
{
Log.d("MyApp", "Server encountered an error");
}
}
catch(Exception e)
{
Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show();
}
//Convert response to string
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF8"));
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
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());
}
//END Convert response to string
try
{
jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++)
{
json_data = jArray.getJSONObject(i);
outputStream.append( "\n"+"Id="+json_data.getString("id") +"\t"+"Name=" + json_data.getString("name") + "\n");
result += "\n" + jArray.getJSONObject(i);
}
}
catch(JSONException e1)
{
e1.printStackTrace();
}
catch (ParseException e1)
{
e1.printStackTrace();
}
}
}
Index.php
<?php
mysql_connect("localhost","root","");
mysql_select_db("android");
$sql=mysql_query("SELECT * FROM table_1 ");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
?>
在運行該應用程序之前,應啟動localserver(xamp)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.