android - 基本认证 android UrlConnection Get

标签 android json authentication

所以我正在制作一个测试登录应用程序,这样我就可以查看应用程序和服务器如何一起运行。 所以我有三个正在使用的端点。 这三个都下载了 json 文件,所以我可以毫无问题地打印出来。 问题是它们都需要用户名和密码。我得到了第一个下载没问题,它工作得很好。我在我的 httpClass 中使用内置的身份验证器来处理这个问题。我所要做的就是将我的用户名和密码传递给它,它就可以正常工作。

另一个端点将检查用户是否可以登录到服务器。这也返回一个 json。它还需要用户名和密码。但它不适用于我用于第一个的验证器。它一直给我 filenotfound 异常和它。我唯一想让它工作的是构建 url 并将其发送到 httpclass。通过像这样手动添加用户名和密码

http://mywebpage/endpoint1

然后像这样添加信息

http://mywebpage/endpint2

我正在使用代码手动执行此操作。我认为这不是正确的方法,因为我开始工作的第一个方法看起来是正确的方法。任何建议,将不胜感激。

这是我正在使用的 html 类

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

private String user;
private String pass;
Activity act;

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

}

@Override
protected String doInBackground(String... params) {
        String url = params[0];
        return http(url);
}
private String http(String myUrl)

{
    String respone = null;
    HttpURLConnection urlConnection = null;
    try
    {
        URL url = new URL(myUrl);
            Authenticator.setDefault(new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(user, pass.toCharArray());
                }
            });

        urlConnection = (HttpURLConnection)url.openConnection();

        InputStream inputStream = urlConnection.getInputStream();

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

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

    }finally {
        if(urlConnection != null)
        {
            urlConnection.disconnect();
        }
    }
    return respone;
}

这是我正在做所有测试的主要类(class)

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.username);
    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);

            }
        });
    }



}

最佳答案

在 android 中使用 Volley 库。它与 GSON 一起使用以发出自定义请求,因此您无需担心 JSON 创建和 JSON 解析。这一切都非常简单。

看看这些链接 http://developer.android.com/training/volley/request-custom.html

关于android - 基本认证 android UrlConnection Get,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36853964/

相关文章:

java - 如何在 Android 操作系统中通过 Java 访问设备驱动程序?

java - Spring Boot 401身份验证错误的日志用户名/密码

android - JSONArray 到 HashMap

java - 广播接收器使程序崩溃

javascript - 将操作添加到 DataTables JSON 特定单元格中

java - net.sf.json.JSONObject 在不需要的地方添加反斜杠?

javascript - 在 Squarespace 上托管 d3.js 可视化

javascript - 登录单页 angularjs Web 应用程序

apache - lighttpd:身份验证后如何将端口(仅对本地主机可见)转发到WAN?

android - 以编程方式连接到 Android Q 中的 Wifi