gpt4 book ai didi

java - Android NameValuePair 发送值

转载 作者:行者123 更新时间:2023-11-29 22:09:02 25 4
gpt4 key购买 nike

我有连接到 MySql 并将数据编码为 JSON 的 PHP 代码。稍后我会过滤它并获得特定的 JSON 对象。当我使用一个 NameValuePair 对象时它工作正常,但现在我想使用用户名和密码等变量。现在我在 logcat 中收到此警报 Error parsing data .org.json.JSONException: Value null of type org.json.JSONObject$1 cannot be converted to JSONArray.

我应该如何更改可以正常工作的代码?

$q=mysql_query("SELECT username, firstname, lastname, email, phone1, skype, city, description FROM mdl_user WHERE username LIKE '$username' AND password LIKE '$password'");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));

发送请求的代码:

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("usern",""+usr));
nameValuePairs.add(new BasicNameValuePair("passw",""+psw));
InputStream is = null;
String result = "";
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://ik.su.lt/~jbarzelis/Bdarbas/getUserInfo.php");
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());
}

已编辑

$username = mysql_real_escape_string($_REQUEST['usern']);
$password = mysql_real_escape_string($_REQUEST['passw']);
$q=mysql_query("SELECT username, firstname, lastname, email, phone1, skype, city, description
FROM mdl_user WHERE username LIKE '$username' AND password LIKE '$password'");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));

最佳答案

如果这是您放在服务器端的全部代码,那么我想您需要先使用 将值输入 $username$password >$_POST[] 这样的方法

$username=$_POST["usern"];
$password=$_POST["passw"];

密码也是如此。

因为现在变量中没有值,您的 SELECT 语句返回 null 值,该值以 JSON 格式发送给客户端给出一个 null 值错误。

关于java - Android NameValuePair 发送值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10095761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com