0

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

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

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


在 postExecute 方法中 sb.tostring之后为null

练习.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类

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

          }

                }}
4

1 回答 1

0

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

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

于 2012-04-06T14:22:51.367 回答