我有关于我的 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。然后,我发现由于google android honeycomb,我不能直接使用这种方法。所以我需要添加 AsyncTask<>,这里是代码
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);
}
}