خرید بک لینک

Vote count: 0

So I am making a test login app so I can use look at how the app and the server are behaving together. So I have three endpoints that I am working with. All three download json file so I can just print that out no problem. The issue is they all require a user name and password. I got the first one to download no problem and it works perfectly. I am using the androids built in authenticator in my httpClass to deal with this. All I have to do is pass my useame and password to it and it works just fine.

Another endpoint is going to check if the user can login into the server. This also retus a json. It also requires a useame and password. but it will not work with the authenticator i used for the first one. It keeps giving me filenotfound exception and that it. The only want I can get this one to work is to build the url and send it to the httpclass. By manually adding the useame and password like this

http://mywebpage/mobileCustomers/validatePassword

and then add the information like this

http://mywebpage/mobileCustomers?email=matttest@test.com&password=qazxsw123

I am doing this manually with code. I do not feel that this is the right way to do this because the first one I got working looks like the right way of doing this. Any suggestions would be appreciated.

This is my html class that I am using

public class MyHttpGet extends AsyncTask<String, Void, String> {

private String useame;
private String password;
Activity activity;

//this is used if you need a password and useame
//mainly for logins to a webserver
public MyHttpGet(String useame, String password)
{
    this.useame = useame;
    this.password = password;

}

//used for image downloading
public MyHttpGet(){}


@Override
protected String doInBackground(String... params) {
        String url = params[0];
        retu httpDownloadData(url);
}


//this is used for a simple http download of json files or xml file
//it must retu a string fom the http request
private String httpDownloadData(String myUrl)

{
    String respone = null;
    HttpURLCoection urlCoection = null;
    try
    {
        URL url = new URL(myUrl);
        //put in the useame and pssword for parmas to send to url
        //this is good for login

            Authenticator.setDefault(new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    retu new PasswordAuthentication(useame, password.toCharArray());
                }
            });

        urlCoection = (HttpURLCoection)url.openCoection();

        InputStream inputStream = urlCoection.getInputStream();

        if(inputStream != null)
        {
            respone = streamToString(inputStream);
            inputStream.close();
        }

    }catch (IOException ie)
    {
        //ie.printStackTrace();
        Log.d("Issue with Http: " , "IOException");
        ie.printStackTrace();

    }finally {
        if(urlCoection != null)
        {
            urlCoection.discoect();
        }
    }
    retu respone;
}

And this is the main class that I am doing all my testing

public class MainActivity extends AppCompatActivity {

EditText user, pass;
TextView reuslt;

String myJson;
String param1 = "?email=";
String param2 = "&password=";

String email, password;

String customerInfo, loginUrl, promo;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    customerInfo = "http://mywebpage/mobileCustomers";
    loginUrl = "http://mywebpage/mobileCustomers/validatePassword";
    promo = "http://a mywebpage/promotions";


    user= (EditText)findViewById(R.id.useame);
    pass = (EditText)findViewById(R.id.password);
    reuslt = (TextView)findViewById(R.id.results);
    Button promotion = (Button)findViewById(R.id.button);
    Button crmTest = (Button)findViewById(R.id.user);
    Button loginTest = (Button)findViewById(R.id.login);

    if (promotion != null) {
        promotion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                email = user.getText().toString();
                password = pass.getText().toString();


                Log.d("User:" , email);
                Log.d("Pass: ", password);


                MyHttpGet task = new MyHttpGet(email, password);
                try {
                    myJson = task.execute(promo).get();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }catch (NullPointerException np)
                {
                    np.printStackTrace();
                }

                reuslt.setText(myJson);

            }
        });
    }

    if (crmTest != null) {
        crmTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String email = user.getText().toString();
                String password = pass.getText().toString();

                email = param1 + email;
                password = param2 + password;

                Log.d("User:" , email);
                Log.d("Pass: ", password);
                String url = customerInfo+email+password;
                Log.d("Url" , url);
                MyHttpGet task = new MyHttpGet(email, password);
                try {
                    myJson = task.execute(url).get();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }catch (NullPointerException np)
                {
                    np.printStackTrace();
                }

                reuslt.setText(myJson);

            }
        });
    }

    if (loginTest != null) {
        loginTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String email = user.getText().toString();
                String password = pass.getText().toString();
                email = param1 + email;
                password = param2 + password;

                Log.d("User:" , email);
                Log.d("Pass: ", password);
                String url = loginUrl+email+password;
                Log.d("Url" , url);

                MyHttpGet task = new MyHttpGet(email, password);
                try {
                    myJson = task.execute(url).get();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }catch (NullPointerException np)
                {
                    np.printStackTrace();
                }

                reuslt.setText(myJson);

            }
        });
    }



}

}

asked 3 mins ago

برچسب: نویسنده: استخدام کار تاريخ: سه شنبه 7 ارديبهشت 1395 ساعت: 9:02

صفحه بندی