我的Android应用程序根据用户名和密码从PHP文件读取JSON数据从应用程序也作为JSON发送
我的问题是,是否可以使用JSONParse读取和过帐?
请看一下JSONParse方法,我添加了一个已注释的部分,因为它不起作用“是否可以使用JSON发送用户名和密码以及从数据库中读取状态”

JSONParse

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

$con=mysqli_connect("localhost","root","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='osama' AND password='123'");
            $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);


?>

最佳答案

用它来保存到数据库

nameValuePairs2 = new  ArrayList<NameValuePair>();
     nameValuePairs2.add(new BasicNameValuePair("username",username));
    nameValuePairs2.add(new BasicNameValuePair("password",password));
 try{
                 HttpClient httpclient = new DefaultHttpClient();
                 HttpPost httppost = new HttpPost(mainurl+"registration.php");
                 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                 HttpResponse response = httpclient.execute(httppost);
                 String the_string_response = convertResponseToString(response);
             //    Toast.makeText(getApplicationContext(), "Response " + the_string_response, Toast.LENGTH_LONG).show();
             }catch(Exception e){
                 //  Toast.makeText(getApplicationContext(), "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show();
                //   System.out.println("Error in http connection "+e.toString());
             }

09-07 16:45