为什么从MYSQL返回到android应用程序的结果是null?

时间:2012-04-06 11:57:28

标签: php android json

我试图在我的数据库中读取数据,然后在我的应用程序中使用它,因为我使用PHP脚本作为Web服务连接到我的Mysql数据库,但我的程序在Json的解析中抛出异常我试图删除解析部分并在日志上打印结果值并显示为null,为什么结果值为null?什么是错误?

我已经在网上搜索并尝试了所有可能的解决方案,但问题仍然存在,请帮助我......我能做些什么来解决这个问题?

这是我打印结果值时的日志:

  

空     在sb.tostring之后      postExecute方法中的null

Exercise.php

   <?php
     $Name=$_REQUEST['name'];               
     mysql_connect("localhost","user","pass");
    mysql_select_db("MYDB");
   $sql=mysql_query("select  burnedCalories   from Exercise where  name='$Name' ");
   while($row=mysql_fetch_assoc($sql))
   $output=$row;
    print(json_encode($output));
     mysql_close();
     ?>

java class

    public class  ExercisesActivity  extends Activity{
            private    String    selected_ex_Name ="Walking";
            private int  calorie_factor;
            public void onCreate(Bundle savedInstanceState) 
            {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.exercise_details);
                new ConnectTask().execute();


                 }

    private class  ConnectTask extends AsyncTask<Void,Void,String>
     {    
 private  String result="";
 private  InputStream is=null;
 protected String doInBackground(Void... params) {


 try
 {
     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
 nameValuePairs.add(new BasicNameValuePair("name",selected_ex_Name));
 HttpClient httpclient = new DefaultHttpClient();
 HttpPost httppost = new HttpPost("http://10.0.2.2/Exercise.php");
 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
 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,"utf-8"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        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());
         }

        Log.e("log_tag", result+ "after sb.tostring  ");
        return result;
        }


 protected  void onPostExecute(String  result){

               Log.e("log_tag", result+" in postExecute method ");

            //           try{
     //                 JSONArray jArray = new JSONArray(result);
   //                   JSONObject json_data=null;
  //                    for(int i=0;i<jArray.length();i++)
  //                    {
  //                        json_data = jArray.getJSONObject(i);
  //                                calorie_factor=json_data.getInt("burnedCalories");
           //                   }
            //  
            //               }
            //                  catch(JSONException e){
             //                 Log.e("log_tag", "Error parsing data  "+e.toString());
              //                    }

          }

                }}

1 个答案:

答案 0 :(得分:0)

您的php脚本位于http://10.0.2.2/Exercise.php,因此可能在某些本地计算机上。即使您可以从浏览器访问它并显示JSON输出,您的应用也无法访问它。所以把它放在外部服务器上并从那里连接到它 - 你必须将HttpPost httppost = new HttpPost("http://10.0.2.2/Exercise.php");中的URL更改为新的。

执行此操作时,可以检查您是否确实有一些可访问的JSON输出,因为从您的日志看起来,似乎您没有。