2011-09-14 16 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にユーザー名とパスワードを挿入されている、buat私はログインできない、誰も私を助けることができますか?ありがとうございます。

+0

ここでログインしていますか?サーバー側かモバイル?ログインに成功するとサーバーから何かが戻ってきますか? –

+0

私はモバイルからログインしたいと思う、サーバーはちょうどログインのために使用されるユーザー名とパスワードを保持する、あなたはそれを行う方法を知っていますか? – Handy

答えて

0

ここでパラメータを入力しますか?

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

私はこのコードを使ってデータベース(username.settext()とpassword.settext())のすべてのユーザー名とパスワードを表示しましたが、動作しますが、このコードを使用してログインできるかどうかはわかりません。あなたはそれをやる方法を知っていますか? – Handy

+0

投稿要求に適切なパラメータを追加していないようです。あなたのコードから伝えるのは難しいですが、要求を実行する前に少なくとも「number_id」をparamに追加する必要があるようです。 –

+0

私はどのような種類のコードを表示できますか?私は本当にこの問題について、理解していないよ – Handy

関連する問題