2013-12-13 119 views
0

我的Android应用根据用户名和密码的应用也为JSON发送从PHP文件中读取JSON数据。
我已经达到了什么,现在通过指定的用户名和密码,没有从应用程序发送的用户名和密码怎么..

我的问题是,是否有可能阅读并POST使用JSONParse,我只能读?
请看看在JSONParse方法我已经添加了,因为它不工作多数民众赞成评论的一部分“是否有可能从数据库读取状态发送用户名和密码一起使用JSON”发送及阅读使用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); 


?> 

回答

2

使用,对于保存到数据库

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

我不想救他们,我只是想沿着侧读取状态发送用户名和密码 – user1928775

+0

ü希望只读JSON数据.... PLZ发布乌尔JSON文件数据 –

+0

JSON数据作为对象PHP文件 – user1928775