这是我的android studio的代码
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
String result = null;
InputStream is = null;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("friend", friend));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.101/webservice/gethismessage.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
JSONArray jArray = new JSONArray(result);
String[] usernameq = new String[jArray.length()];
String[] message = new String[jArray.length()];
for (int i = 0; i < jArray.length(); i++) {
usernameq[i] = jArray.getJSONObject(i).getString("sentuser");
message[i] = jArray.getJSONObject(i).getString("message");
if (usernameq[i] == username) {
ChatMessage chatMessage = new ChatMessage().setLocal(true).setsentuser(true).setContent(message[i]);
addMessage(chatMessage);
} else {
ChatMessage chatMessage = new ChatMessage().setLocal(true).setsentuser(false).setContent(message[i]);
addMessage(chatMessage);
}
}
这是我的PHP代码
<?php
$username ='root';
$password ='lean96022198';
$hostname ='localhost';
$database ='test_xmpp';
$localhost = mysql_connect($hostname,$username,$password) or
trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database,$localhost);
$username = $_POST["username"]
$friend = $_POST["friend"]
$i=mysql_query("select sentuser,message from messagehistory where (sentuser= $username and receiveuser= $friend) or (sentuser= $friend and receiveuser= $username)");
$num_rows = mysql_num_rows($i);
while($row = mysql_fetch_array($i))
$r[]=$row;
print(json_encode($r));
echo"hello"
?>
我在这两个脚本中出错
usernameq[i] = jArray.getJSONObject(i).getString("sentuser");
message[i] = jArray.getJSONObject(i).getString("message");
请帮我修复我的php或android代码的脚本,以便我可以获得&#34; sentuser&#34;的值。和&#34;消息&#34;成功地。我只知道如何从db中选择一个值来获取值。