mysql的or和android,Android与MySQL的连接

我正在开发一个最后一年的项目,我需要将Android模拟器与MySQL数据库连接以检索值。 Java文件:

public class connectivity extends Activity {

/** Called when the activity is first created. */

TextView txt;

public static final String KEY_121 = "http://10.0.2.2/mysqlcon.php";

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

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));

}

private String getServerData(String returnString) {

InputStream is = null;

String result = null;

// the year data to send

ArrayList nameValuePairs = new ArrayList();

nameValuePairs.add(new BasicNameValuePair("year", "1970"));

// 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 = "0";

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);

JSONObject json_data = null;

for (int i = 0; i < jArray.length(); i++) {

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 += "nt" + jArray.getJSONObject(i);

}

} catch (JSONException e) {

Log.e("log_tag", "Error parsing data " + e.toString());

}

return returnString;

}

}

我还在Android清单文件中提供了互联网许可。但是在运行应用程序后,我在logcat中收到以下错误:

ERROR PARSING DATA org.json.JSONException:JSONArraytext必须以字符0处的'['开头

我认为这表明返回了一个空值。请帮助我,因为这是我的最后一年项目。我花了几个小时试图找到解决方案,但它没有用。

我目前正在使用Android 2.2。 wamp服务器位于localhost上,因此我使用的地址为10.0.2.2,这是localhost(127.0.0.1)的特殊别名。任何帮助将非常感激。

这是PHP代码:

mysql_connect("127.0.0.1","root","chetan");

mysql_select_db("db1");

$q=mysql_query("SELECT * FROM people WHERE birthyear>'".$_REQUEST['year']."'");

while($e=mysql_fetch_assoc($q))

$output[]=$e;

print(json_encode($output,JSON_FORCE_OBJECT));

mysql_close();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值