簡體   English   中英

json不會從android中的服務器數據庫返回任何值

[英]json doesn't return any value from the server database in android

我正在嘗試通過Android Phpserver database檢索一些值。 服務器中的行與php中where子句中的條件匹配,但json數組返回為空。 我的PHP代碼如下。 我認為$username變量沒有收到值,因為當我初始化$ username =“ 11111”時,顯示了json數組。請逐步向我解釋問題所在。

<?php

mysql_connect("localhost","uname","pswd");
mysql_select_db("demo");    


$username= (isset($_POST['receivenumber'])) ? $_POST['receivenumber'] : '';
$q=mysql_query("SELECT `TaskId` FROM `addtasknew` where `receivenumber` = '$username'") or die(mysql_error());

 $output=array(); 
 while($e=mysql_fetch_assoc($q))

  $output[]=$e;

 print (json_encode($output));

 mysql_close();

 ?>

Android代碼

    final String name=c.get("ph");

 AsyncTask<Void, Void,Void> t = new AsyncTask<Void, Void,Void>()
     {

        @Override
        protected Void doInBackground(Void... arg0) {
  String result = null;
InputStream is = null;
try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.mydomainname.org/task/senttask.php");
        ArrayList<NameValuePair>  nameValuePairs = new ArrayList<NameValuePair>();

        nameValuePairs.add(new BasicNameValuePair("receivenumber",name.toString()));
        //nameValuePairs.add(new BasicNameValuePair("receivenumber","9595959595"));
        HttpResponse response = httpclient.execute(httppost); 
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpEntity entity = response.getEntity();
        is = entity.getContent();

        Log.e("log_tag", "connection success "+nameValuePairs);

}
catch(Exception e)
{
        Log.e("log_tag", "Error in http connection "+e.toString());
        Toast.makeText(getActivity(), "Connection fail", Toast.LENGTH_SHORT).show();

}
try
{
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,HTTP.UTF_8),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) 
        {
                sb.append(line + "\n");

        }
        is.close();

        result=sb.toString();
      Log.e("log_tag", "result "+result.toString());
}
catch(Exception e)
{
       Log.e("log_tag", "Error converting result "+e.toString());
    Toast.makeText(getActivity(), " Input reading fail", Toast.LENGTH_SHORT).show();

}
try
{
    JSONArray jArray = new JSONArray(result);
      String s="",s1,s2,s3,s4,s5;
      Log.w("Lengh",""+jArray.length());
        for(int i=0;i<jArray.length();i++){

         // final ListView lview=(ListView) rootView.findViewById(R.id.listViewmytask);
            JSONObject json_data = jArray.getJSONObject(i);

              s=json_data.getString("TaskId");

               Log.e("taskid from server","s");

        }



}
catch(JSONException e)
{
        Log.e("log_tag", "Error parsing data "+e.toString());
        Toast.makeText(getActivity(), "JsonArray fail", Toast.LENGTH_SHORT).show();
}
return null;
        }
     };
     t.execute();

您的請求密鑰與您期望的不同。

receivenumber != sendernumber

另外,你永遠不會通過nameValuePairshttppost 您需要這樣的東西:

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM