我的Android客户端有问题。
我有一个脚本,它使用了一个宁静的Web服务并确定用户身份验证,这是我的代码
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.AsyncTask;
import android.util.Log;
public class RestLogin {
private static String URL = "http://REST Webservice here?";
private static final String tag = "Logcat tag: ";
public static Boolean Auth(String username,String password)
{
String result = "";
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet(URL);
//adding parameter to request to determine user authentication
request.addHeader("username",username);
request.addHeader("password",password);
//taking handler to get execution status
ResponseHandler<String> handler = new BasicResponseHandler();
try
{
result = httpclient.execute(request, handler);
}
catch (ClientProtocolException cpe)
{
cpe.printStackTrace();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
catch(IllegalArgumentException iae)
{
iae.printStackTrace();
}
catch(NullPointerException npe)
{
npe.printStackTrace();
}
httpclient.getConnectionManager().shutdown();
Log.i(tag,result);
return Boolean.parseBoolean(result);
}
}
首先,当我构建代码时,它在我的logcat状态上显示错误NetworkOnMainThreadException。然后,我发现自谷歌android蜂窝以来,我无法直接使用这种方法。所以我需要添加AsyncTask&lt;&gt;,这是代码
public class RestLogin extends AsyncTask<String, String, Boolean> {
private static String URL = "http://Restful webservice here?";
private static final String tag = "Logcat tag: ";
@Override
protected Boolean doInBackground(String... params) {
//TO DO Auto Generated Method
return null;
}
现在我卡住了,我怎么能用AsyncTask调用Auth()方法?
提前感谢。
好的,更新:这是新语法
public class RestLogin extends AsyncTask<Void, Void, Void> {
private static String URL = "http://REST Webservice here?";
private static final String tag = "Logcat tag: ";
private String usname, psswrd;
public RestLogin(String username, String password){
usname = username;
psswrd = password;
}
protected void onPreExecute(){
super.onPreExecute();
}
protected Void doInBackground(Void... params) {
Boolean stat = Auth(usname,psswrd);
return null;
}
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
}
public static Boolean Auth(String username,String password)
{
String result = "";
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet(URL);
//adding parameter to request to determine user authentication
request.addHeader("username",username);
request.addHeader("password",password);
//taking handler to get execution status
ResponseHandler<String> handler = new BasicResponseHandler();
try
{
result = httpclient.execute(request, handler);
}
catch (ClientProtocolException cpe)
{
cpe.printStackTrace();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
catch(IllegalArgumentException iae)
{
iae.printStackTrace();
}
catch(NullPointerException npe)
{
npe.printStackTrace();
}
httpclient.getConnectionManager().shutdown();
Log.i(tag,result);
return Boolean.parseBoolean(result);
}
}
答案 0 :(得分:1)
试试这个:
public class RestLogin extends AsyncTask<String, Void, Boolean> {
private static String URL = "http://Restful webservice here?";
private static final String tag = "Logcat tag: ";
@Override
protected Boolean doInBackground(String... params) {
String username = params[0]; //username
String password = params[1]; //password
//now call Auth
return Auth(username,password);
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if(result){
//successful login. Display a Toast or take the user to next screen by using intent.
}
else {
//Display Authentication failed error message.
}
}
public Boolean Auth(String username,String password)
{
// your logic.
}
}
可以通过以下代码调用AsyncTask。
new RestLogin().execute(username,password);
如果您需要更多帮助,请与我们联系。
答案 1 :(得分:0)
发生NetworkOnmainthreadException,因为您正在主UI线程上运行与网络相关的操作。您应该使用 Asynctask 。
当应用程序尝试在其主线程上执行网络操作时引发的异常。
仅针对Honeycomb SDK或更高版本的应用程序进行此操作。针对早期SDK版本的应用程序可以在其主要事件循环线程上进行网络连接,但是非常不鼓励这样做。请参阅文档设计响应性。
http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html
您可以使用asycntask。 http://developer.android.com/reference/android/os/AsyncTask.html
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new TheTask(username,password).execute();
}
class TheTask extends AsyncTask<Void,Void,Void>
{
String username,pwd;
public TheTask(String uname, String password)
{
username=uname;
pwd=password;
}
protected void onPreExecute()
{ super.onPreExecute();
//display progressdialog.
}
protected void doInBackground(Void ...params)
{
//rest web service. do not update ui here
boolean somevalue= Auth(username, pwd);
return null;
}
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
//dismiss progressdialog.
//update ui
}
}