接收JSON列表时出错

时间:2013-12-13 10:14:22

标签: php android mysql json

我正在使用JSON同时做两件事首先我要发送(用户名和密码)到PHP文件,从用户名和密码读取数据库中的切换按钮状态,PHP文件也发送切换状态使用JSON List到应用程序 我将读取和发送用户名和密码结合起来,但它始终在Logcat中显示错误

JSON代码我正在使用组合发送用户和密码并根据该用户名和密码接收JSON列表

private class JSONParse extends AsyncTask<String, String, JSONObject> {
             private ProgressDialog pDialog;
            @Override
            protected void onPreExecute() {
                super.onPreExecute();

                pDialog = new ProgressDialog(fishtank.this);
                pDialog.setMessage("Getting Data ...");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(true);
                pDialog.show();

            }

            @Override
            protected JSONObject doInBackground(String... args) {

            /*Temp
                SharedPreferences settings =  getSharedPreferences("mySettings", Activity.MODE_PRIVATE);
                 us = settings.getString("storedWifiUser", "");
                    ps = settings.getString("storedWifiPass", "");
                 try {
                     JSONObject json = new JSONObject(); 
                     json.put("user", us); 
                     json.put("pass", ps);

                     postData(json);


                 } catch (JSONException e) {
                     e.printStackTrace();}  Temp */
                JSONParser jParser = new JSONParser();   
                // Getting JSON from URL
                JSONObject json = jParser.getJSONFromUrl(url);
                return json;
            }

           /* public void postData(JSONObject json) throws JSONException {
                HttpClient httpclient = new DefaultHttpClient();

                try { 
                    HttpPost httppost = new HttpPost(url);

                    List<NameValuePair> nvp = new ArrayList<NameValuePair>(2);    
                    nvp.add(new BasicNameValuePair("json", json.toString()));
                    //httppost.setHeader("Content-type", "application/json");  
                    httppost.setEntity(new UrlEncodedFormEntity(nvp));
                    HttpResponse response = httpclient.execute(httppost); 
                    Log.i("JSON Response : ",json.toString().trim());
                    if(response != null) {
                        InputStream is = response.getEntity().getContent();
                        //input stream is response that can be shown back on android
                    }

                }catch (Exception e) {
                    e.printStackTrace();
                } 
            } */
            //Temp
             @Override

             protected void onPostExecute(JSONObject json) {
                 pDialog.dismiss();
                 try {
                    // Log.i("JSON Response : ",json.toString().trim());
                    // Log.i("JSON Response : "+json.toString().trim());

                    // System.out.println("JSON Response : "+json.toString().trim());

                     //Temp
                     JSONObject json2= new JSONObject();
                     SharedPreferences settings =  getSharedPreferences("mySettings", Activity.MODE_PRIVATE);
                     us = settings.getString("storedWifiUser", "");
                        ps = settings.getString("storedWifiPass", "");
                     try {

                         json.put("user", us); 
                         json.put("pass", ps);

                     } catch (JSONException e) {
                         e.printStackTrace();}
                     HttpClient httpclient = new DefaultHttpClient();

                        try { 
                            HttpPost httppost = new HttpPost(url);

                            List<NameValuePair> nvp = new ArrayList<NameValuePair>(2);    
                            nvp.add(new BasicNameValuePair("json", json2.toString()));
                            //httppost.setHeader("Content-type", "application/json");  
                            httppost.setEntity(new UrlEncodedFormEntity(nvp));
                            HttpResponse response = httpclient.execute(httppost); 
                            Log.i("JSON Response : ",json.toString().trim());
                            if(response != null) {
                                InputStream is = response.getEntity().getContent();
                                //input stream is response that can be shown back on android
                            }

                        }catch (Exception e) {
                            e.printStackTrace();
                        } 

                     //Temp
                     JSONObject c = json.getJSONObject("status");
                     String tog1="";
                     String tog2="";
                     String tog3="";
                     if(c.has("fil"))

                            tog1 = c.getString("fil");

                        if(c.has("HEA"))
                         tog2 = c.getString("HEA");

                        if(c.has("LED"))
                            tog3 = c.getString("LED");

                        Log.i("JSON Response : ",json.toString().trim());
                                        if(tog1.equals("ON"))
                                        {   toggle1.setChecked(true);}
                                        else{ toggle1.setChecked(false);}
                                             if(tog2.equals("ON"))
                                             {   toggle2.setChecked(true);}
                                             else{   toggle2.setChecked(false);}
                                             if(tog3.equals("ON"))
                                             {    toggle3.setChecked(true);}
                                             else{toggle3.setChecked(false);}

                             } catch (JSONException e) {
                                 e.printStackTrace();
                             }

             }






        }//End Json

PHP文件

    <?php ("Content-type:text/css")

$con=mysqli_connect("localhost","test","123","pet_home");
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

// if(isset($_POST['json'])){
        $json=$_POST['json'];
        $data2=json_decode($json,TRUE);
        $u=$data2->{'user'};
        $p=$data2->{'pass'};
            $result = mysqli_query($con,"SELECT * FROM users WHERE username='$u' AND password='$p'");
            $row_cnt = mysqli_num_rows($result);
//$row_cnt=1;
             if($row_cnt>0){     
                 $row = mysqli_fetch_array($result); 
                $data = array('success'=>true, 'error'=>'',  'status'=>array("fil" => $row['filter_st'], "HEA"=> $row['heat_st'], "LED" =>$row['led_st'])); 
             }else{
                $data = array('success'=>false, 'error' => 'No records found');
             }               

   //  }else{
    //     $data = array('success'=>false, 'error' => 'No POST value from Android App');
  //   }

     echo json_encode($data);

mysqli_close($con);


?>

logcat的

    12-13 10:54:43.118: E/AndroidRuntime(8940): FATAL EXCEPTION: main
12-13 10:54:43.118: E/AndroidRuntime(8940): java.lang.NullPointerException
12-13 10:54:43.118: E/AndroidRuntime(8940):     at com.set.petshome.fishtank$JSONParse.onPostExecute(fishtank.java:334)
12-13 10:54:43.118: E/AndroidRuntime(8940):     at com.set.petshome.fishtank$JSONParse.onPostExecute(fishtank.java:1)
12-13 10:54:43.118: E/AndroidRuntime(8940):     at android.os.AsyncTask.finish(AsyncTask.java:631)
12-13 10:54:43.118: E/AndroidRuntime(8940):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
12-13 10:54:43.118: E/AndroidRuntime(8940):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
12-13 10:54:43.118: E/AndroidRuntime(8940):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-13 10:54:43.118: E/AndroidRuntime(8940):     at android.os.Looper.loop(Looper.java:137)
12-13 10:54:43.118: E/AndroidRuntime(8940):     at android.app.ActivityThread.main(ActivityThread.java:5103)
12-13 10:54:43.118: E/AndroidRuntime(8940):     at java.lang.reflect.Method.invokeNative(Native Method)
12-13 10:54:43.118: E/AndroidRuntime(8940):     at java.lang.reflect.Method.invoke(Method.java:525)
12-13 10:54:43.118: E/AndroidRuntime(8940):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-13 10:54:43.118: E/AndroidRuntime(8940):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-13 10:54:43.118: E/AndroidRuntime(8940):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

根据Log,你的回复中会出现一些像“
”这样的html标签,所以在php header("Content-type: text/css")中设置标题,然后再试一次。