lawang code

lawang Code


Tanyakan kode disini

Android Webservice

ditanyakan oleh adhit 4 tahun yang lalu

mastah...., ane mau nanya ttg android nih... ane pgn insertin data ke mySQL server..., gada error sih, tapi kok datannya ga nyampe ya..., ni kodingnya :

public class RegisterActivity extends Activity {
    private final String NAMESPACE = "http://androidWebservice.TA.com";
    private final String URL = "http://10.0.2.2:8080/androidWebservice/services/AndroidService?wsdl";
    private final String SOAP_ACTION = "http://androidWebservice.TA.com/registration";
    private final String METHOD_NAME = "registration";


    Button daftar, cancel;
    TextView status;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        daftar = (Button)findViewById(R.id.btn_reg_daftar);
        daftar.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                registrationAction();
            }
        });
    }

    private void registrationAction() throws NullPointerException {
        BigInteger pubkeyA;
        Random rand1 = new Random(System.currentTimeMillis());
        Random rand2 = new Random(System.currentTimeMillis()*10);

        BigInteger p = BigInteger.probablePrime(32, rand1);
        BigInteger g = BigInteger.probablePrime(32, rand2);

        int a = rand1.nextInt(256);

        pubkeyA = g.modPow(BigInteger.valueOf(a), p);

        // TODO Auto-generated method stub
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        EditText userName = (EditText) findViewById(R.id.et_reg_nama);
        String user_Name = userName.getText().toString();

        EditText userPassword = (EditText) findViewById(R.id.et_reg_password);
        String user_Password = userPassword.getText().toString();
        int ascii = 0;
        int [] hasilArray=new int[user_Password.length()];
        for(int i=0;i<user_Password.length();i++){
            char character=user_Password.charAt(i);
            ascii=(int) character;
            hasilArray[i]=ascii;
        }


        EditText userEmail = (EditText) findViewById(R.id.et_reg_email);
        String user_email = userEmail.getText().toString();

      //Pass value for userName variable of the web service
        PropertyInfo unameProp =new PropertyInfo();
        unameProp.setName("userName");//Define the variable name in the web service method
        unameProp.setValue(user_Name);//set value for userName variable
        unameProp.setType(String.class);//Define the type of the variable
        request.addProperty(unameProp);//Pass properties to the variable

      //Pass value for Password variable of the web service
        PropertyInfo passwordProp =new PropertyInfo();
        passwordProp.setName("password");
        passwordProp.setValue(ascii);
        passwordProp.setType(String.class);
        request.addProperty(passwordProp);

      //Pass value for email variable of the web service
        PropertyInfo emailProp =new PropertyInfo();
        emailProp.setName("email");
        emailProp.setValue(user_email);
        emailProp.setType(String.class);
        request.addProperty(emailProp);

      //Pass value for p
        PropertyInfo pProp = new PropertyInfo();
        pProp.setName("pVal");
        pProp.setValue(p);
        request.addProperty(pProp);

      //Pass value for g
        PropertyInfo gProp = new PropertyInfo();
        gProp.setName("gVal");
        gProp.setValue(g);
        request.addProperty(gProp);

      //Pass value for pubkey
        PropertyInfo pubkeyAProp = new PropertyInfo();
        pubkeyAProp.setName("pubkeyAVal");
        pubkeyAProp.setValue(pubkeyA);
        request.addProperty(pubkeyAProp);


        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        try{
            androidHttpTransport.call(SOAP_ACTION, envelope);
               SoapPrimitive response = (SoapPrimitive)envelope.getResponse();

               status = (TextView) findViewById(R.id.tv_reg_status);
               status.setText(response.toString());
               //Intent dashboardIntent = new Intent(this.getApplicationContext(), DashboardActivity.class);
               //startActivityForResult(dashboardIntent, 0); 
               userName.clearComposingText();
               userPassword.clearComposingText();


        }
        catch(Exception e){
            e.printStackTrace();

        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.register, menu);
        return true;
    }

}

ni webservicenya :

public class AndroidService {
    public String authentication(String userName,String password){

          String retrievedUserName = "";
          String retrievedPassword = "";
          String status = "";
          try{
              Class.forName("com.mysql.jdbc.Driver");
              java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/androidapp_login","root","rootadmin");
              java.sql.PreparedStatement statement =  con.prepareStatement("SELECT * FROM useradmin_login WHERE username = '"+userName+"'");
              ResultSet result = statement.executeQuery();

              while(result.next()){
                  retrievedUserName = result.getString("username");
                  retrievedPassword = result.getString("password");
              }

              if(retrievedUserName.equals(userName)&&retrievedPassword.equals(password)){
                  status = "Success!";
              }

              else{
                  status = "Login fail!!!";
              }

          }
          catch(Exception e){
              e.printStackTrace();
          }
          return status;

    }
    public String registration(String userName,String password, String email, BigInteger pVal, BigInteger gVal, BigInteger pubkeyAVal){

          //String retrievedUserName = "";
          //String retrievedPassword = "";
          //String retrievedEmail="";
          //String status = "";
          try{
              Class.forName("com.mysql.jdbc.Driver");
              java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/androidapp_login","root","rootadmin");
              java.sql.PreparedStatement statement =  con.prepareStatement("INSERT INTO useradmin_login (ID,username, password,email,pval, gval, pubkey) VALUES (ID,'"+userName+"','"+password+"','"+email+"','"+pVal+"','"+gVal+"','"+pubkeyAVal+"') ");
              int result = statement.executeUpdate();


          }
          catch(Exception e){
              System.out.println(e.getMessage());
          }
          return "Insertion Successfull";     
    }
}

gada error mastah..., tapi data ga nyampe ke database..., mohon pencerahan :)

Menampilkan 1-6 dari 6 hasil
handita okviyanto menjawab 4 tahun yang lalu :

Kalau lihat kodenya sih kayaknya udah bener. Responsenya dapatnya apa emangnya? di kode ini .. status.setText(response.toString());

Kenapa gak pake REST aja lebih gampang pake JSON

Udah ditambah juga kan di permissionnya ?

    <uses-permission android:name="android.permission.INTERNET" />


adhit menjawab 4 tahun yang lalu :

responnya buat yg register it insertion success..., webservice buat loginnya sig udh brhasil buat login..., permissionnya juga udah kok...,

ane dpt tutorial json yang marsingnya pake PHP, bingung om..., ane maunya java..., biar ga dobel2 blajarnya... hehehe..., ada tutorialnya ga mastah?? boleh dong dibagi :)


handita okviyanto menjawab 4 tahun yang lalu :

Ane dlu pernah buat tutorial di sini tapi untuk GET datanya.. untuk POST belum sempet buat tutorialnya. Pake JSON. http://mrhandsblog.blogspot.com/2011/06/ambil-data-dari-server-di-android.html

Coba dibaca terlebih dahulu.. ane dulu juga pernah nyoba kayak gitu pake Java semua. Tapi setelah mau live nyari server yang Java susah akhirnya pake PHP dan MySQL deh..

Kalo ngerti pake framework buatnya lebih gampang saran ane sih pake CI atau Yii. Walaupun belajar lagi tetapi pastinya akan selalu berguna di kemudian hari.

Ini salah satu contoh untuk yang pake Yii http://www.yiiframework.com/wiki/175/how-to-create-a-rest-api/


adhit menjawab 4 tahun yang lalu :

hmm..., bukannya apache tomcat tuh server java ya mastah??

ada satu modul yang ane gabisa di phpnya..., dan itu cukup memusingkan..., wkwkwkwk


handita okviyanto menjawab 4 tahun yang lalu :

Iya maksudnya mau hosting online nyari yang support apache tomcat lebih susah daripada nyari apache untuk phpnya.. hehe gapapa juga sih senengan mau yang mana..


adhit menjawab 4 tahun yang lalu :

owalahhh..., di ane mah rncana cm lokal doang sih..., gaperlo hostingan jadi..., hehehe


Anda harus login terlebih dahulu untuk memberikan komentar

Recent Tag

X 6
X 48
X 14
X 10
X 1
X 5
X 4
X 15
X 10

Bantuan

Help

Feedback

Help