我一直在嘗試這個教程。它沒有顯示任何錯誤,但在模擬器上只顯示我給出的URL!將android連接到mysql數據庫
我的連接字符串是:
public static final String KEY_121 = "http://10.0.2.2/index.php";
(我用10.0.2.2作爲我工作的本地主機上)
當我檢查了我的logcat這表明我DIS:
error parsing data org.json.JsonException: Value <br of type java.lang.String cannot be converted to JSONArray
我真的不知道我的代碼有什麼問題。誰能幫幫我嗎。我最後一年的項目真的需要幫助。
這是我的Android的Java代碼
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.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 main extends Activity {
/** Called when the activity is first created. */
TextView txt1, txt2;
@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.
txt1 = (TextView) findViewById(R.id.ip);
LinearLayout rootLayout = new LinearLayout(getApplicationContext());
txt1 = new TextView(getApplicationContext());
rootLayout.addView(txt1);
setContentView(rootLayout);
// Set the text and call the connect function.
txt1.setText("Connecting...");
// call the method to run the data retreival
txt1.setText(getServerData(KEY_121));
}
public static final String KEY_121 = "http://10.0.2.2/index.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("year", "1970"));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(KEY_121);
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",
"longitude: " + json_data.getDouble("longitude")
+ ", latitude: "
+ json_data.getDouble("latitude"));
// 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代碼
<?php
mysql_connect("localhost","root","sugi");
mysql_select_db("android");
$q=mysql_query("SELECT * FROM people WHERE birthyear>'".$_REQUEST['year']."'");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));
mysql_close();
?>
這是MySQL查詢
CREATE TABLE people (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
name VARCHAR(100) NOT NULL ,
sex BOOL NOT NULL DEFAULT '1',
birthyear INT NOT NULL
)
這是當我輸入什麼它顯示「http://10.0.2.2/index.php 「on IE
嗨,非常感謝幫助。我是PHP和MySQL的新手。你能告訴我,我需要做什麼來發送我的請求? – sugianto 2011-04-12 01:56:17