dongtu1357 2012-04-10 20:11
浏览 54
已采纳

Android NameValuePair发送值

I have PHP code which connects to MySql and encodes data to JSON. Later I would filter it and get specific JSON object. It worked fine while I was using one NameValuePair object but now I want to use variables like username and password. Now I am getting this alert in logcat Error parsing data .org.json.JSONException: Value null of type org.json.JSONObject$1 cannot be converted to JSONArray.

How should I change the code that could work properly ?

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

Code which sends request:

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

Edited

$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));
  • 写回答

2条回答 默认 最新

  • dongzha3058 2012-04-10 20:16
    关注

    If this is the entire code that you have put on your server side then i guess you need to first take values into $username and $password using the $_POST[] method like this

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

    and the same for password.

    since as of now there is no value in the variables your SELECT statement returns null value which is sent to client in JSON format which gives a null value error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题