所以我正在制作一个测试登录应用程序,所以我可以看看应用程序和服务器如何一起表现。 所以我有三个与我合作的端点。 所有三个下载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;
}
这是我正在进行所有测试的主要课程
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);
}
});
}
}
}
答案 0 :(得分:1)
在android中使用Volley库。它与GSON一起用于发出自定义请求,因此您无需担心JSON创建和JSON解析。一切都很容易。
查看这些链接http://developer.android.com/training/volley/request-custom.html