我想通过以下教程连接到Android设备的MySQL:http://www.anddev.org/networking-database-problems-f29/connecting-to-mysql-database-t50063.html。无法使用PHP连接到MySQL从Android使用PHP
所有这一切发生的是,它正在打印我希望连接到模拟器上的服务器的IP地址。它显示了logcat的以下错误:
12-08 11:42:01.993: I/dalvikvm(274): threadid=3: reacting to signal 3
12-08 11:42:02.113: I/dalvikvm(274): Wrote stack traces to '/data/anr/traces.txt'
12-08 11:42:03.273: E/log_tag(274): Error parsing data org.json.JSONException: Value
<!DOCTYPE of type java.lang.String cannot be converted to JSONArray
12-08 11:45:58.283: E/log_tag(351): Error parsing data org.json.JSONException: Value
<!DOCTYPE of type java.lang.String cannot be converted to JSONArray
12-08 11:53:11.302: E/log_tag(378): Error parsing data org.json.JSONException: Value
<!DOCTYPE of type java.lang.String cannot be converted to JSONObject
12-08 12:03:31.643: E/log_tag(405): Error parsing data org.json.JSONException: Value
<!DOCTYPE of type java.lang.String cannot be converted to JSONArray
12-08 12:20:57.052: E/log_tag(432): Error parsing data org.json.JSONException: Value
<!DOCTYPE of type java.lang.String cannot be converted to JSONArray
下面是我的java文件的副本:
package com.david.Connect;
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 ConnectActivity 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 retreival
txt.setText(getServerData(KEY_121));
}
public static final String KEY_121 = "http://86.47.59.249/employee.php";
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("code","1"));
//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("EmployeeId")+
", name: "+json_data.getString("First_Name")+
", sex: "+json_data.getInt("Last_Name")+
", birthyear: "+json_data.getInt("Birth_Date")
);
//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;
}
}
,这里是我的PHP文件,我有服务器86.47.59.29在MySQL中数据库是:
<?php
mysql_connect("86.47.59.249","username","password");
mysql_select_db("Test");
$q=mysql_query("SELECT * FROM Tbl_Employee WHERE
EmployeeId>'".mysql_real_escape_string ($_REQUEST['code'])."'");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));
mysql_close();
?>
而下面是我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.david.Connect"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".ConnectActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
任何人都可以对此有所了解吗?它让我疯狂!
感谢
您应该紧急更改您的php脚本 - 将'$ _REQUEST ['code']'更改为'mysql_real_escape_string($ _REQUEST ['code'])''。如果没有这种改变,任何阅读这篇文章的人都会有足够的信息来轻易地破坏你的数据库,并且可能会使用SQL注入攻击你的服务器看到这个页面的背景阅读:http://php.net/manual/en/security.database.sql-injection.php –
谢谢你只是改变那里 – DMC