Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I have tried your code it looks perfect but not able to hit api i don't know the issue please take a look #1

Open
SameerKhan1406 opened this issue Nov 8, 2016 · 1 comment

Comments

@SameerKhan1406
Copy link

SameerKhan1406 commented Nov 8, 2016

Login Activity
package name

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;


import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private static String TAG = LoginActivity.class.getSimpleName();

    private PrefManager pref;
    private Button mLoginButton,btnRequestSms;
    private EditText mLoginNo,inputOtp;
    private ProgressDialog mSpinner;
    private CheckBox mBox;
    private SQLiteDatabase db;
    private TextView otptext;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        createDatabase();
        mBox = (CheckBox) findViewById(R.id.checkBox1);
        otptext= (TextView) findViewById(R.id.otptext);
        inputOtp=(EditText) findViewById(R.id.inputOtp);
        String checkBoxText = "I agree to all the <a href='http://www.redbus.in/mob/mTerms.aspx' > Terms and Conditions</a>";

        mBox.setText(Html.fromHtml(checkBoxText));
        pref = new PrefManager(this);
        mBox.setMovementMethod(LinkMovementMethod.getInstance());
        btnRequestSms = (Button) findViewById(R.id.btn_request_sms);
        mLoginButton = (Button) findViewById(R.id.loginButton);
        mBox.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mBox.isChecked()){
                    btnRequestSms.setVisibility(View.VISIBLE);
                }
                else{
                    mLoginButton.setVisibility(View.GONE);
                    btnRequestSms.setVisibility(View.GONE);
                    otptext.setVisibility(View.GONE);
                    inputOtp.setVisibility(View.GONE);
                }
            }
        });
        ;
        btnRequestSms.setOnClickListener((OnClickListener) this);
        mLoginButton.setOnClickListener((OnClickListener) this);
        mLoginNo = (EditText) findViewById(R.id.loginNo);
        mLoginButton.setEnabled(false);
        mLoginButton.setOnClickListener((OnClickListener) this);

        // Checking for user session
        // if user is already logged in, take him to main activity
        if (pref.isLoggedIn()) {
            Intent intent = new Intent(LoginActivity.this, PlaceCallActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);

            finish();
        }

    }
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_request_sms:
                otptext.setVisibility(View.VISIBLE);
                inputOtp.setVisibility(View.VISIBLE);
                mLoginButton.setVisibility(View.VISIBLE);
                validateForm();
                verifyOtp();
                break;

            case R.id.loginButton:

                loginClicked();
                break;

        }
    }
    protected void createDatabase(){
        db=openOrCreateDatabase("NumberDB", Context.MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS numbers(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, number INT(4));");
    }
    protected void insertIntoDB(){
        String number = mLoginNo.getText().toString();
        if(number.equals("")){
            Toast.makeText(getApplicationContext(),"Please fill all fields", Toast.LENGTH_LONG).show();
            return;
        }

        String query = "INSERT INTO persons (name,address) VALUES("+number+");";
        db.execSQL(query);
        Toast.makeText(getApplicationContext(),"Saved Successfully", Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onPause() {
        if (mSpinner != null) {
            mSpinner.dismiss();
        }
        super.onPause();
    }

    @Override
    public void onStartFailed(SinchError error) {
        Toast.makeText(this, error.toString(), Toast.LENGTH_LONG).show();
        if (mSpinner != null) {
            mSpinner.dismiss();
        }
    }

    @Override
    public void onStarted() {
        openPlaceCallActivity();
    }

    private void loginClicked() {
        String userNo = mLoginNo.getText().toString();

        if (userNo.isEmpty()) {
            Toast.makeText(this, "Please enter your number", Toast.LENGTH_LONG).show();
            return;
        }
    }

    private void showSpinner() {
        mSpinner = new ProgressDialog(this);
        mSpinner.setTitle("Logging in");
        mSpinner.setMessage("Please wait...");
        mSpinner.show();
    }

    private void validateForm() {

        String mobile = mLoginNo.getText().toString().trim();

        // validating mobile number
        // it should be of 10 digits length
        if (isValidPhoneNumber(mobile)) {

            // saving the mobile number in shared preferences
            pref.setMobileNumber(mobile);

            // requesting for sms
            requestForSMS(mobile);

        } else {
            Toast.makeText(getApplicationContext(), "Please enter valid mobile number", Toast.LENGTH_SHORT).show();
        }
    }


    private void requestForSMS(final String mobile) {
        StringRequest strReq = new StringRequest(Request.Method.POST,
                Config.URL_REQUEST_SMS, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                Log.d(TAG, response.toString());

                try {
                    JSONObject responseObj = new JSONObject(response);

                    // Parsing json object response
                    // response will be a json object
                    boolean error = responseObj.getBoolean("error");
                    String message = responseObj.getString("message");

                    // checking for error, if not error SMS is initiated
                    // device should receive it shortly
                    if (!error) {
                        // boolean flag saying device is waiting for sms
                        pref.setIsWaitingForSms(true);

                        Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();

                    } else {
                        Toast.makeText(getApplicationContext(),
                                "Error: " + message,
                                Toast.LENGTH_LONG).show();
                    }

                    // hiding the progress bar


                } catch (JSONException e) {
                    Toast.makeText(getApplicationContext(),
                            "Error: " + e.getMessage(),
                            Toast.LENGTH_LONG).show();


                }

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }) {

            /**
             * Passing user parameters to our server
             * @return
             */
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("mobile", mobile);

                Log.e(TAG, "Posting params: " + params.toString());

                return params;
            }

        };

        // Adding request to request queue
        MyApplication.getInstance().addToRequestQueue(strReq);
    }

    /**
     * sending the OTP to server and activating the user
     */







    private void verifyOtp() {
        String otp = inputOtp.getText().toString().trim();

        if (!otp.isEmpty()) {
            Intent grapprIntent = new Intent(getApplicationContext(), HttpService.class);
            grapprIntent.putExtra("otp", otp);
            startService(grapprIntent);
        } else {
            Toast.makeText(getApplicationContext(), "Please enter the OTP", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * Regex to validate the mobile number
     * mobile number should be of 10 digits length
     *
     * @param mobile
     * @return
     */


    private static boolean isValidPhoneNumber(String mobile) {
        String regEx = "^[0-9]{10}$";
        return mobile.matches(regEx);
    }
   
}

Login xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="0dip"
    tools:context=".LoginActivity">

    <RelativeLayout
        android:id="@+id/layout_sms"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView android:src="@drawable/bg"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_marginTop="40dp"
            android:adjustViewBounds="true"
            android:layout_centerHorizontal="true"
            />


        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingLeft="40dp"
            android:paddingRight="40dp"
            android:id="@+id/linearLayout">

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="200dp"
                android:text="Enter your Mobile No"
                android:textAllCaps="true"
                android:textColor="@color/purple"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/loginNo"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:background="@drawable/inputbox"
                android:digits="1234567890"
                android:inputType="phone"
                android:padding="10dp"
                android:textColor="@color/purple"
                android:textSize="25sp"
                android:maxLength="10">

                <requestFocus />
            </EditText>

        </LinearLayout>

        <Button
            android:id="@+id/btn_request_sms"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="210dp"
            android:text="NEXT"
            android:background="@color/btnclr"
            android:minHeight="30dp"
            android:minWidth="150dp"
            android:textColor="@color/off_white"
            android:textSize="14dp"
            android:textStyle="bold"
            android:visibility="gone" />



        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="20dp"
            android:textColor="@color/black"
            android:buttonTint="@color/black"/>
        </RelativeLayout>

        <LinearLayout
            android:id="@+id/layout_otp"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center_horizontal"
            android:orientation="vertical"
            android:layout_marginTop="350dp"
            >

            <TextView
                android:id="@+id/otptext"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:gravity="center_horizontal"
                android:paddingLeft="40dp"
                android:paddingRight="40dp"
                android:text="@string/msg_manual_otp"
                android:textColor="@android:color/black"
                android:textSize="12dp"
                android:visibility="gone"/>

            <EditText
                android:id="@+id/inputOtp"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:background="@drawable/inputbox"
                android:fontFamily="sans-serif-light"
                android:gravity="center_horizontal"
                android:hint="@string/lbl_enter_otp"
                android:inputType="number"
                android:maxLength="6"
                android:padding="10dp"
                android:textCursorDrawable="@null"
                android:textSize="18dp"
                android:layout_marginTop="10dp"
                android:visibility="gone"/>
            <Button
                android:id="@+id/loginButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="15dp"
                android:text="VERIFY"
                android:background="@color/btnclr"
                android:minHeight="30dp"
                android:minWidth="150dp"
                android:textColor="@color/off_white"
                android:textSize="14dp"
                android:textStyle="bold"
                android:visibility="gone" />

        </LinearLayout>


</RelativeLayout>
@SameerKhan1406
Copy link
Author

SameerKhan1406 commented Nov 8, 2016

in Android Monitor i am getting this
11-08 15:12:25.835 19058-19301/appid E/LoginActivity: Posting params: {mobile=7704052254}
11-08 15:12:25.845 19058-19301/appid D/libc-netbsd: [getaddrinfo]: hostname=192.168.0.19; servname=(null); cache_mode=(null), netid=0; mark=0
11-08 15:12:25.846 19058-19301/appid D/libc-netbsd: [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=4; ai_family=0
11-08 15:12:25.847 19058-19301/appid D/libc-netbsd: [getaddrinfo]: hostname=192.168.0.19; servname=(null); cache_mode=(null), netid=0; mark=0
11-08 15:12:25.847 19058-19301/appid D/libc-netbsd: [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=4; ai_family=0
11-08 15:12:25.849 19058-19301/appid I/System.out: [CDS]rx timeout:2500
11-08 15:12:25.850 19058-19301/appid I/System.out: [socket][2] connection /192.168.0.19:80;LocalPort=41088(2500)
11-08 15:12:25.850 19058-19301/appid I/System.out: [CDS]connect[/192.168.0.19:80] tm:2
11-08 15:12:25.853 19058-19301/appid D/Posix: [Posix_connect Debug]Process com.medmainfomatix.form :80
11-08 15:12:25.910 19058-19085/appid D/GraphicBuffer: register, handle(0xb7915ab8) (w:275 h:66 s:288 f:0x1 u:0x000f02)
11-08 15:12:25.911 19058-19085/appid I/MaliEGL: [Mali]window_type=1, is_framebuffer=0, errnum = 0
11-08 15:12:25.911 19058-19085/appid I/MaliEGL: [Mali]surface->num_buffers=4, surface->num_frames=3, win_min_undequeued=1
11-08 15:12:25.911 19058-19085/appid I/MaliEGL: [Mali]max_allowed_dequeued_buffers=3
11-08 15:12:25.912 19058-19085/appid D/GraphicBuffer: unregister, handle(0xb7915ab8) (w:275 h:66 s:288 f:0x1 u:0x000f02)
11-08 15:12:25.914 19058-19085/appid D/GraphicBuffer: register, handle(0xb7915ab8) (w:275 h:66 s:288 f:0x1 u:0x000f02)
11-08 15:12:27.843 19058-19085/appid D/OpenGLRenderer: Flushing caches (mode 0)
11-08 15:12:27.843 19058-19085/appid D/OpenGLRenderer: Flushing caches (mode 0)
11-08 15:12:27.843 19058-19085/appid D/GraphicBuffer: unregister, handle(0xb7915ab8) (w:275 h:66 s:288 f:0x1 u:0x000f02)
11-08 15:12:28.356 19058-19301/com.medmainfomatix.form I/System.out: [socket][3:41088] exception
11-08 15:12:28.358 19058-19301/appid I/System.out: [CDS]close[41088]
11-08 15:12:28.360 19058-19301/appid E/LoginActivity: Posting params: {mobile=7704052254}
11-08 15:12:28.361 19058-19301/appid D/libc-netbsd: [getaddrinfo]: hostname=192.168.0.19; servname=(null); cache_mode=(null), netid=0; mark=0
11-08 15:12:28.361 19058-19301/appid D/libc-netbsd: [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=4; ai_family=0
11-08 15:12:28.362 19058-19301/appid D/libc-netbsd: [getaddrinfo]: hostname=192.168.0.19; servname=(null); cache_mode=(null), netid=0; mark=0
11-08 15:12:28.362 19058-19301/appid D/libc-netbsd: [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=4; ai_family=0
11-08 15:12:28.365 19058-19301/appid I/System.out: [CDS]rx timeout:5000
11-08 15:12:28.365 19058-19301/appid I/System.out: [socket][3] connection /192.168.0.19:80;LocalPort=44276(5000)
11-08 15:12:28.366 19058-19301/appid I/System.out: [CDS]connect[/192.168.0.19:80] tm:5
11-08 15:12:28.368 19058-19301/appid D/Posix: [Posix_connect Debug]Process com.medmainfomatix.form :80
11-08 15:12:33.379 19058-19301/appid I/System.out: [socket][4:44276] exception
11-08 15:12:33.380 19058-19301/appid I/System.out: [CDS]close[44276]
11-08 15:12:33.382 19058-19058/appid E/LoginActivity: Error: null
11-08 15:12:33.391 19058-19058/appid D/Volley: [1] Request.finish: 7557 ms: [ ] http://192.168.0.19/myname/AndroidOtp/request_sms.php 0x6768b565 NORMAL 2
11-08 15:12:33.484 19058-19085/appid D/GraphicBuffer: register, handle(0xb78f4920) (w:78 h:66 s:80 f:0x1 u:0x000f02)
11-08 15:12:33.485 19058-19085/appid I/MaliEGL: [Mali]window_type=1, is_framebuffer=0, errnum = 0
11-08 15:12:33.487 19058-19085/appid I/MaliEGL: [Mali]surface->num_buffers=4, surface->num_frames=3, win_min_undequeued=1
11-08 15:12:33.488 19058-19085/appid I/MaliEGL: [Mali]max_allowed_dequeued_buffers=3
11-08 15:12:33.490 19058-19085/appid D/GraphicBuffer: unregister, handle(0xb78f4920) (w:78 h:66 s:80 f:0x1 u:0x000f02)
11-08 15:12:33.494 19058-19085/appid D/GraphicBuffer: register, handle(0xb7935898) (w:78 h:66 s:80 f:0x1 u:0x000f02)
11-08 15:12:35.392 19058-19085/appid D/OpenGLRenderer: Flushing caches (mode 0)
11-08 15:12:35.393 19058-19085/appid D/OpenGLRenderer: Flushing caches (mode 0)
11-08 15:12:35.393 19058-19085/appid D/GraphicBuffer: unregister, handle(0xb7935898) (w:78 h:66 s:80 f:0x1 u:0x000f02)
11-08 15:14:37.817 19058-19085/appid D/OpenGLRenderer: Flushing caches (mode 0)
11-08 15:14:37.818 19058-19085/appid D/OpenGLRenderer: Flushing caches (mode 0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant