2011-09-14 3 views
1

로그인 폼, 내 코드는 다음과 같다 :JSON 내가 JSON 인코딩하여 로그인 양식을 해왔

public void onClick(View v) { 
if(v==login){ 
login(); 
} 
} 

public void login(){ 

      String url_select = "http://ipadress/select.php"; 

      HttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url_select); 

      //parameter 
      ArrayList<NameValuePair> param = new ArrayList<NameValuePair>(); 

      try { 
       //add parameter 
       httpPost.setEntity(new UrlEncodedFormEntity(param)); 

      HttpResponse httpRespose = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpRespose.getEntity(); 

      //read content 
      InputStream in = httpEntity.getContent(); 
      BufferedReader read = new BufferedReader(new InputStreamReader(in)); 

      String content = ""; 
      String line = ""; 

      while((line = read.readLine())!=null){ 
       content += line; 
      } 

      Log.d("ADBUG", "content: "+content); 


      //json 
      if(!content.equals("null")){ 

       try { 
        JSONArray jArr = new JSONArray(content); 
        String uname=""; 
        String passw=""; 
        String idx=""; 
        for(int i=0;i<jArr.length();i++){ 
         JSONObject jObj = jArr.getJSONObject(i); 
         String id = jObj.getString("_id"); 
         String username = jObj.getString("username"); 
         String password = jObj.getString("password"); 
         idx += id+"\n"; 
         uname += username+"\n"; 
         passw += password+"\n"; 
        } 

        if(user.getText().toString().equals(uname) && pass.getText().toString().equals(passw)){ 
         Intent a; 
         a = new Intent(this, WelcomeMenu.class); 
         startActivity(a); 
        } 


       } catch (JSONException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

      }else{ 
       Toast.makeText(this, "Error", Toast.LENGTH_LONG).show(); 
      } 

      } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      } 
     } 

select.php :

<?php 
include_once("koneksi.php"); 
$number_id= $_REQUEST['number_id']; 

if($nomor_id){ 
    $sqlString = "select * from login where _id=$number_id"; 
}else{ 
    $sqlString = "select * from login"; 
} 

$rs = mysql_query($sqlString); 

if($rs){ 
    while($objRs = mysql_fetch_assoc($rs)){ 
     $output[] = $objRs; 
    } 

    echo json_encode($output); 
} 

mysql_close(); 

?> 

문제가 내가입니다 수 없어 로그인, 나는 이미 내 SQL에 사용자 이름과 암호를 넣었습니다, 내가 로그인 할 수 없기 때문에 누구든지 나를 도울 수 있습니까? 감사합니다.

+0

로그인을 수행 하시겠습니까? 서버 측 또는 모바일? 성공적인 로그인을 위해 서버에서 반환되는 것이 있습니까? –

+0

나는 모바일에서 로그인하고 싶다. 서버는 로그인에 사용될 사용자 이름과 패스워드 만 가지고있다. 어떻게 할 수 있는지 알고 있니? – Handy

답변

0

어디에서 매개 변수를 채우겠습니까?

 ArrayList<NameValuePair> param = new ArrayList<NameValuePair>(); 
+0

나는이 코드를 사용하여 데이터베이스 (username.settext() 및 password.settext())에 모든 사용자 이름과 암호를 표시하려고 시도했지만 작동하지만이 코드를 사용하여 로그인 할 수 있는지 여부는 잘 모르겠습니다. . 그것을하는 법을 아십니까? – Handy

+0

게시 요청에 적절한 매개 변수를 추가하지 않는 것 같습니다. 코드에서 알기는 어렵지만 요청을 실행하기 전에 적어도 'number_id'를 param에 추가해야하는 것처럼 보입니다. –

+0

어떤 종류의 코드를 보여줄 수 있습니까? 나는이 문제에 대해 정말로 이해하지 못한다. 감사 전에 – Handy