[英]Restful api with CURL in android
我正在制作需要登录身份验证的应用程序。 我有api:
curl --user username:password -X GET -http://sample.com
现在,我想验证登录名并获得响应。
我已经考虑这个 ,但我没有得到任何结果。
我已经做到了。 但是它继续前进。
public static String getRequest() {
StringBuffer stringBuffer = new StringBuffer("");
BufferedReader bufferedReader = null;
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet();
URI uri = new URI("http://sample.com");
httpGet.setURI(uri);
httpGet.addHeader(BasicScheme.authenticate(
new UsernamePasswordCredentials("username", "password"),
HTTP.UTF_8, false));
HttpResponse httpResponse = httpClient.execute(httpGet);
InputStream inputStream = httpResponse.getEntity().getContent();
bufferedReader = new BufferedReader(new InputStreamReader(
inputStream));
String readLine = bufferedReader.readLine();
while (readLine != null) {
stringBuffer.append(readLine);
stringBuffer.append("\n");
readLine = bufferedReader.readLine();
}
} catch (Exception e) {
// TODO: handle exception
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
// TODO: handle exception
}
}
}
return stringBuffer.toString();
}
我怎样才能做到这一点?
提前致谢。
您在主线程上调用网络操作。 这种情况下崩溃。 使用凌空库或在AsyncTask或线程中使用您的代码。 记住要在主线程中更新用户界面。
考虑使用Android异步Http客户端 。 这样可以简化您的生活。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.