2014-03-05 46 views
0

我試過這段代碼。在onclick工作正常,但在我的輸出中,我的json值沒有顯示在輸出字段,, ,但是當我用childname.setText(result);替換json代碼時可以得到如下屏幕截圖所示的輸出 我想分割這些值並在相應的輸入區域中顯示它。 建議PLZ ,,,,在此先感謝安卓在我的輸入字段分裂json輸出,

enter image description here

我Javafile

public class ChildProfile extends Activity { 
    private TextView childname,childid; 

    private Button get; 
    private EditText username,password; 
    private JSONObject jObj; 
    private static String user,pass,json,child,id; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.child_profile); 
     username=(EditText) findViewById(R.id.profile_username); 
     password=(EditText) findViewById(R.id.profile_password); 
     childname=(TextView) findViewById(R.id.profile_email); 
     childid=(TextView) findViewById(R.id.profile_childid); 
     get=(Button) findViewById(R.id.profile_button1); 
     get.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       valuess(); 
       if(!user.equals("") & !pass.equals("")) 
       { 
       new sendPostData().execute(); 
       } 
       else if(pass.equals("") & user.equals("")) 
       { 
        Toast toast=Toast.makeText(ChildProfile.this,"Enter Username & Password", Toast.LENGTH_LONG); 
         toast.show(); 
       } 
       else if(user.equals("")) 
       { 
        Toast toast=Toast.makeText(ChildProfile.this,"Enter Username", Toast.LENGTH_LONG); 
         toast.show(); 
       } 
       else if(pass.equals("")) 
       { 
        Toast toast=Toast.makeText(ChildProfile.this,"Enter Password", Toast.LENGTH_LONG); 
         toast.show(); 
       } 

      } 
     }); 
    } 
    private class sendPostData extends AsyncTask<String, Void, String> 
    { 
      @Override 
     protected String doInBackground(String... params) { 

    try{ 
        valuess(); 
        String link="http://192.168.1.6:81/arun/value.php"; 
        String data = URLEncoder.encode("username", "UTF-8") 
        + "=" + URLEncoder.encode(user, "UTF-8"); 
        data += "&" + URLEncoder.encode("password", "UTF-8") 
        + "=" + URLEncoder.encode(pass, "UTF-8"); 
        URL url = new URL(link); 
        URLConnection conn = url.openConnection(); 
        conn.setDoOutput(true); 
        OutputStreamWriter wr = new OutputStreamWriter 
        (conn.getOutputStream()); 
        wr.write(data); 
        wr.flush(); 
        BufferedReader reader = new BufferedReader 
        (new InputStreamReader(conn.getInputStream())); 
        StringBuilder sb = new StringBuilder(); 
        String line = null; 
        // Read Server Response 
        while((line = reader.readLine()) != null) 
        { 
         sb.append(line); 
         break; 
        } 
        return sb.toString(); 
       }catch(Exception e){ 
        return new String("Exception: " + e.getMessage()); 
       } 

     } 
      @Override 
     protected void onPostExecute(String result) { 
      //View your result here. 
       /* if(result.equals("wrong")) 
       { 
        Toast toast=Toast.makeText(ChildProfile.this,"Invalid account.Enter correct username and password or Please SignUp", Toast.LENGTH_LONG); 
        toast.show(); 
       } 
       else 
       { 
        Toast toast=Toast.makeText(ChildProfile.this,"Invalid account.Enter correct username and password or Please SignUp", Toast.LENGTH_LONG); 
        toast.show();    
       } 
       json=result; 
       try { 
       jObj = new JSONObject(result); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      catch (NullPointerException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      try { 
       child=jObj.getString("ChildPassportName"); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      catch (NullPointerException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      try { 
       id=jObj.getString("ChildID"); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      catch (NullPointerException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      childid.setText(id); 
      Toast toast=Toast.makeText(ChildProfile.this,"Invalid account.Enter correct username and password or Please SignUp", Toast.LENGTH_LONG); 
      toast.show();*/ 
     childname.setText(result); 
      } 
    } 
    public void valuess() 
    { 
     user = username.getText().toString(); 
     pass = password.getText().toString(); 
    } 
    } 

我的PHP文件

<?php 
$con=mysql_connect("localhost","arun","sachin11"); 

$db_select = mysql_select_db('Schoolapp', $con); 
$username = $_POST['username']; 
$password = $_POST['password']; 
$result = mysql_query("SELECT ChildPassportName,ChildID FROM SchoolDB where Username='$username' and Password='$password'",$con); 
while($r = mysql_fetch_assoc($result)) { 
    $rows[] = $r; 
} 
print json_encode($rows); 

?> 

PHP輸出

[{"ChildPassportName":"Arun","ChildID":"0"}] 
+0

您需要解析該json響應。 –

回答

0

試試這個..

你得到的結果爲JSONArray所以你必須解析它。

 @Override 
     protected void onPostExecute(String result) { 

      String id = ""; 
      String name = ""; 

      try { 
        JSONArray new_array = new JSONArray(result);  
        JSONObject obj = new_array.getJSONObject(0); 
        id = obj.getString("ChildID"): 
        name = obj.getString("ChildPassportName"): 
      }catch (JSONException e) { 
       // TODO Auto-generated catch block 
        e.printStackTrace(); 
      } 

      childid.setText(id); 
      childname.setText(name); 
     } 
+0

它的工作原理,,,,我試過這段代碼,但忘記轉換爲json數組 @hariharan ,,,謝謝 –

+0

@ArunShankar很高興幫助。快樂編碼。 – Hariharan