我在 Java 和网络编程中使用 nube。但我必须为 android 编写应用程序,该应用程序与 http 一起工作。我为http请求编写了类,并且有一个问题:授权后错误401,当我发送另一个请求时。它是什么蜂,我该如何解决?谢谢您的帮助
public void SendRequest(final String url) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
URI website = null;
try {
website = new URI(baseUri+url);
} catch (URISyntaxException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = null;
try {
response = client.execute(request, localContext);
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
if (response != null)
{
HttpEntity entity = response.getEntity();
try {
InputStream is = entity.getContent();
httpEvent.HttpResponseArrived(convertStreamToString(is),response.getStatusLine().getStatusCode());
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
});
t.start();
}
public void Login(String login, String password, boolean isRemember)
{
String mess = "logon?username="+login+"&password="+password+"&remember="+ Boolean.toString(isRemember);
SendRequest(mess);
httpEvent = new HttpHandler()
{
@Override
public void HttpResponseArrived(String data, int code)
{
String mess = "Error";
switch (code)
{
case 401:
mess = "Incorrect login/password";
break;
case 200:
mess = "";
break;
}
events.LoginArrived(Boolean.valueOf(data), mess);
}
};
}