2014-09-26 3 views
0

mysql에서 데이터를 가져 오는 .java 클래스를 만들고 있습니다. mysql에서 가져온 데이터가 모바일에 로그인 한 userID를 기반으로하는지 어떻게 확인할 수 있습니까? 여기 MySQL의 모든 데이터를 얻기 위해 실행 내 코드입니다 :안드로이드 : 매개 변수를 사용하여 mysql에서 sqlite로 데이터를 가져 오는 방법

package a.a; 
    import java.util.concurrent.ExecutionException; 
    import org.json.JSONArray; 
    import org.json.JSONObject; 
    import android.app.Activity; 
    import android.app.AlertDialog; 
    import android.content.Context; 
    import android.content.DialogInterface; 
    import android.content.Intent; 
    import android.os.Bundle; 
    import android.text.format.Time; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.Button; 
    import android.widget.Toast; 

    public class Update_Rolling extends Activity 
    { 
     Database b = new Database(this); 
     Button yes, no; 
     String result; 
     String dealID, dealName, dealOwner, dealWil, dealTelp, dealKtgr, dealSales; 
     String Status1, userName; 
     int prDate, prMonth, prYear; 

     // -------- INTERNET -------------- 
     // flag for Internet connection status 
     Boolean isInternetPresent = false; 

     // Connection detector class 
     ConnectionDetector cd; 

     public void onCreate(Bundle savedInstanceState) 
     { 
      super.onCreate(savedInstanceState); 
     setContentView(R.layout.empty); 

     b.open(); 
     yes = (Button)findViewById(R.id.yes); 
     no = (Button)findViewById(R.id.no); 

     //nerima intent 
       Status1 = getIntent().getExtras().getString("status"); 
       userName = getIntent().getExtras().getString("NPK"); 

     cd = new ConnectionDetector(getApplicationContext()); 

     // get Internet status 
     isInternetPresent = cd.isConnectingToInternet(); 


     yes.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // get Internet status 
       isInternetPresent = cd.isConnectingToInternet(); 

       // check for Internet status 
       if (isInternetPresent) { 
       //function update 
        b.deleteDealerList(); 
        b.createListDealer(); 
        getWebRolling(); 

       showAlertDialog(Update_Rolling.this, "Internet Connection", 
          "Data Updated", true); 

       } else { 
        // Internet connection is not present 
        showAlertDialog(Update_Rolling.this, "No Internet Connection", 
          "You don't have internet connection.", false); 
       } 
      } 
     }); 

     no.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Intent aaa = new Intent(Update_Rolling.this, Homescreen.class); 
       aaa.putExtra("status", Status1); 
       aaa.putExtra("NPK", userName); 
       startActivity(aaa); 
      } 
     }); 

    } 

    public void getWebRolling(){ 
     GetAsynctaskData getAsyncData = new GetAsynctaskData(); 
     try 
     { 
     result = getAsyncData.execute("http://10.0.2.2/cibi/viewDealer.php").get(); 

     } catch (InterruptedException e) { 
      // TODO: handle exception 
      e.printStackTrace(); 
     } catch (ExecutionException e) { 
      // TODO: handle exception 
      e.printStackTrace(); 
     } 
     insertRoll(result); 
    } 

    private void insertRoll(String abc) { 
     try { 
      JSONArray json = new JSONArray(abc); 
      for(int i = 0; i < json.length(); i++) 
      { 
       JSONObject json_obj = json.getJSONObject(i);    
       //ambil field data dari XAMPP di .getString 
       dealID = json_obj.getString("CustCode"); 
       dealName = json_obj.getString("Nama"); 
       dealWil = json_obj.getString("Kota"); 
       dealSales = json_obj.getString("Salesman"); 

       String noSales = dealSales.toString(); 
       String noToko = dealID.toString(); 

       Time today = new Time(Time.getCurrentTimezone()); 
       today.setToNow(); 
       prDate = today.monthDay; 
       prMonth = today.month+1; 
       prYear = today.year; 

       String LDno = noSales+""+prYear+""+prMonth+""+noToko+""; 

       Toast.makeText(Update_Rolling.this, LDno, Toast.LENGTH_LONG).show(); 

       //ini cuman insert buat pertama kali 
       b.insertDealABC(dealID, dealName, dealWil); 
       b.insertLD(LDno, dealID, dealSales); 

      } 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      Toast.makeText(Update_Rolling.this, "update database gagal", Toast.LENGTH_LONG).show(); 
      e.printStackTrace(); 
     } 
    } 



    public void showAlertDialog(Context context, String title, String message, Boolean status) { 
     AlertDialog alertDialog = new AlertDialog.Builder(context).create(); 

     // Setting Dialog Title 
     alertDialog.setTitle(title); 

     // Setting Dialog Message 
     alertDialog.setMessage(message); 

     // Setting alert dialog icon 
     //alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail); 

     // Setting OK Button 
     alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       Intent aaa = new Intent(Update_Rolling.this, Homescreen.class); 
       aaa.putExtra("status", Status1); 
       aaa.putExtra("NPK", userName); 
       startActivity(aaa); 
      } 
     }); 
     alertDialog.show(); 
    } 
} 

이 내 .PHP입니다 : viewDealer.php, 즉 당신이 방법에 따라 할 것

<?php 
    include("connect.php"); 
    $query = "SELECT * FROM dealer"; 
    $result = mysql_query($query); 
     while($data = mysql_fetch_array($result)) 
     { 
     $rows[] = $data; 

     } 
     echo json_encode($rows); 
    ?> 

답변

1

음은 테이블 설정. dealer라는 테이블이 있고 해당 ID와 관련된 ID를 원하면 'user_id'열을 추가하여 사용자의 ID를 저장하십시오. 거기에서 사용자가 로그인 할 때 그 ID를 PHP 스크립트에 전달합니다. 그럼 당신은 당신이 아주 많이

result = getAsyncData.execute("http://10.0.2.2/cibi/viewDealer.php?user_id="+varHoldingUserId).get(); 
+0

감사합니다를 사용하는 URL에 사용자 ID를 전달 자바에서이 요청을하려면 그 USER_ID

과 관련된 데이터를 얻을 수

$user_id = strip_tags(stripslashes($_GET['user_id'])); $query="SELECT * FROM dealers where user_id='$user_id'"; 

를 쿼리를 사용할 수 있습니다 ! 그것이 작동합니다 .. – Nes

+0

이 답변은 당신이 찾고 있던 대답은 동의로하십시오. – ksealey

관련 문제