需要帮助,有混淆的AsyncTask [英] need help, got confuse with AsyncTask
本文介绍了需要帮助,有混淆的AsyncTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是新android系统中,我需要做的AsyncTask,所以我的应用程序可以在ICS工作。后来看了教程我还有迷惑。任何人,请帮助我解决我的code,我不知道什么,在哪里我必须投入的AsyncTask我的code这样。谢谢
Login.java
包com.karismaelearning;
公共类登录扩展活动{
公共Koneksi linkurl;
字符串SERVER_URL;
私人按钮登录,注册,设置;
私人的EditText的用户名,密码;
公共ProgressDialog progressDialog;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.login);
设置=(按钮)findViewById(R.id.bsetting);
登录=(按钮)findViewById(R.id.login);
注册=(按钮)findViewById(R.id.reg);
用户名=(EditText上)findViewById(R.id.uname);
密码=(EditText上)findViewById(R.id.pass);
setting.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
意图intentSet =新的意图(Login.this,UrlSetting.class);
startActivity(intentSet);
}
});
register.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
意图intentReg =新的意图(Login.this,Register.class);
startActivity(intentReg);
}
});
login.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
串响应=无效;
。字符串mUsername = username.getText()的toString();
。字符串mPassword = password.getText()的toString();
响应= tryLogin(mUsername,mPassword).trim();
Log.d(检查,在这里);
Log.d(响应,响应);
如果(response.toLowerCase()。包含(berhasil))
{
字符串NAMA = username.getText()的toString()。
意图newIntent =新的意图(Login.this,MainPage.class);
束束=新包();
bundle.putString(非农产品市场准入,NAMA);
newIntent.putExtras(包);
startActivityForResult(newIntent,0);
}
其他
{
//可选
// Kalau BISA dibuat不断untuk menghindari萨拉赫penulisan
字符串RoleError =角色SALAH;
字符串UserError =用户SALAH;
CreateDialog函数(MAAF,response.equals(RoleError)角色安达bukan学生!?!用户名密码Atau萨拉赫);
}
}
});
}
保护字符串tryLogin(字符串mUsername,字符串mPassword)
{
Log.d(TryLoginCheck,在这里);
HttpURLConnection的连接;
OutputStreamWriter请求= NULL;
网址URL = NULL;
串响应=无效;
字符串TEMP = NULL;
字符串参数=用户名=+ mUsername +&放大器;密码=+ mPassword;
的System.out.println(用户名+ mUsername +\ N+密码+ mPassword);
Log.d(参数,参数);
尝试
{
;
linkurl =新Koneksi(本);
SERVER_URL = linkurl.getUrl();
SERVER_URL + =/mobile/Login.php;
URL =新的URL(SERVER_URL);
连接=(HttpURLConnection类)url.openConnection();
connection.setDoOutput(真正的);
connection.setRequestProperty(内容类型,应用程序/ x-WWW的形式urlen codeD);
connection.setRequestMethod(POST);
要求=新OutputStreamWriter(connection.getOutputStream());
request.write(参数);
request.flush();
request.close();
串线=;
InputStreamReader的ISR =新的InputStreamReader(connection.getInputStream());
的BufferedReader读卡器=新的BufferedReader(ISR);
StringBuilder的SB =新的StringBuilder();
而((行= reader.readLine())!= NULL){
sb.append(行+\ N);
}
临时= sb.toString();
Log.d(温度,温度);
响应= sb.toString();
Log.d(响应,响应);
Log.d(锑的价值,sb.toString());
isr.close();
reader.close();
}
赶上(IOException异常E){
Toast.makeText(这一点,e.toString(),Toast.LENGTH_SHORT).show();
}
返回响应;
}
类LoginTask扩展的AsyncTask<字符串,太虚,整数GT; {
公共LoginTask(登录活动,ProgressDialog progressDialog){
}
@覆盖
在preExecute保护无效(){
progressDialog.show();
}
@覆盖
保护整数doInBackground(字符串...为arg0){
}
私人无效CreateDialog函数(字符串标题,文本字符串){
AlertDialog广告=新AlertDialog.Builder(本)
.setPositiveButton(OK,NULL)
.setTitle(标题)
.setMessage(文本)
。创建();
ad.show();
}
}
login.java - 编辑 - >是这样
包com.karismaelearning;
公共类登录扩展活动{
公共Koneksi linkurl;
字符串SERVER_URL;
私人按钮登录,注册,设置;
私人的EditText的用户名,密码;
公共ProgressDialog progressDialog;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.login);
设置=(按钮)findViewById(R.id.bsetting);
登录=(按钮)findViewById(R.id.login);
注册=(按钮)findViewById(R.id.reg);
用户名=(EditText上)findViewById(R.id.uname);
密码=(EditText上)findViewById(R.id.pass);
setting.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
意图intentSet =新的意图(Login.this,UrlSetting.class);
startActivity(intentSet);
}
});
register.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
意图intentReg =新的意图(Login.this,Register.class);
startActivity(intentReg);
}
});
login.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
新LoginTask()执行();
}
});
}
保护字符串tryLogin(字符串mUsername,字符串mPassword){
Log.d(TryLoginCheck,在这里);
HttpURLConnection的连接;
OutputStreamWriter请求= NULL;
网址URL = NULL;
串响应=无效;
字符串TEMP = NULL;
字符串参数=用户名=+ mUsername +&放大器;密码=+ mPassword;
的System.out.println(用户名+ mUsername +\ N+密码+ mPassword);
Log.d(参数,参数);
尝试{
linkurl =新Koneksi(本);
SERVER_URL = linkurl.getUrl();
SERVER_URL + =/mobile/Login.php;
URL =新的URL(SERVER_URL);
连接=(HttpURLConnection类)url.openConnection();
connection.setDoOutput(真正的);
connection.setRequestProperty(内容类型,应用程序/ x-WWW的形式urlen codeD);
connection.setRequestMethod(POST);
要求=新OutputStreamWriter(connection.getOutputStream());
request.write(参数);
request.flush();
request.close();
串线=;
InputStreamReader的ISR =新的InputStreamReader(connection.getInputStream());
的BufferedReader读卡器=新的BufferedReader(ISR);
StringBuilder的SB =新的StringBuilder();
而((行= reader.readLine())!= NULL){
sb.append(行+\ N);
}
临时= sb.toString();
Log.d(温度,温度);
响应= sb.toString();
Log.d(响应,响应);
Log.d(锑的价值,sb.toString());
isr.close();
reader.close();
}
赶上(IOException异常E){
Toast.makeText(这一点,e.toString(),Toast.LENGTH_SHORT).show();
}
返回响应;
}
公共类LoginTask扩展的AsyncTask<字符串,太虚,字符串> {
串响应=无效;
公共LoginTask(){
}
@覆盖
在preExecute保护无效(){
}
@覆盖
保护字符串doInBackground(字符串...为arg0){
。字符串mUsername = username.getText()的toString();
。字符串mPassword = password.getText()的toString();
响应= tryLogin(mUsername,mPassword).trim();
返回响应;
}
保护无效onPostExecute(字符串结果){
super.onPostExecute(结果);
Log.d(检查,在这里);
Log.d(响应,响应);
如果(response.toLowerCase()。包含(berhasil)){
字符串NAMA = username.getText()的toString()。
意图newIntent =新的意图(Login.this,MainPage.class);
束束=新包();
bundle.putString(非农产品市场准入,NAMA);
newIntent.putExtras(包);
startActivityForResult(newIntent,0);
}
其他{
//可选
// Kalau BISA dibuat不断untuk menghindari萨拉赫penulisan
字符串RoleError =角色SALAH;
字符串UserError =用户SALAH;
CreateDialog函数(MAAF,response.equals(RoleError)角色安达bukan学生!?!用户名密码Atau萨拉赫);
}
}
}
私人无效CreateDialog函数(字符串标题,文本字符串){
AlertDialog广告=新AlertDialog.Builder(本)
.setPositiveButton(OK,NULL)
.setTitle(标题)
.setMessage(文本)
。创建();
ad.show();
}
}
解决方案
把你的的OnClick
方法
新LoginTask()执行(stringParam);
单击元素后执行的方法。
I'm new in android and i need to make AsyncTask, so my application can work on ICS. But after I read tutorials i still got confuse. Anyone, please help me to fix my code, i don't know what and where i must put in AsyncTask with my code like this. thank you
Login.java
package com.karismaelearning;
public class Login extends Activity {
public Koneksi linkurl;
String SERVER_URL;
private Button login, register, setting;
private EditText username, password;
public ProgressDialog progressDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
setting = (Button)findViewById(R.id.bsetting);
login = (Button) findViewById(R.id.login);
register = (Button) findViewById(R.id.reg);
username = (EditText) findViewById(R.id.uname);
password = (EditText) findViewById(R.id.pass);
setting.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intentSet = new Intent(Login.this, UrlSetting.class);
startActivity(intentSet);
}
});
register.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intentReg = new Intent(Login.this, Register.class);
startActivity(intentReg);
}
});
login.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String response = null;
String mUsername = username.getText().toString();
String mPassword = password.getText().toString();
response = tryLogin(mUsername, mPassword).trim();
Log.d("Check","Here");
Log.d("Response",response);
if(response.toLowerCase().contains("berhasil"))
{
String nama = username.getText().toString();
Intent newIntent = new Intent(Login.this, MainPage.class);
Bundle bundle = new Bundle();
bundle.putString("nama", nama);
newIntent.putExtras(bundle);
startActivityForResult(newIntent, 0);
}
else
{
//Optional
//Kalau bisa dibuat constant untuk menghindari salah penulisan
String RoleError = "ROLE SALAH";
String UserError = "USER SALAH";
createDialog("Maaf", response.equals(RoleError) ? "Role Anda bukan Student!" : "Username Atau Password Salah!");
}
}
});
}
protected String tryLogin(String mUsername, String mPassword)
{
Log.d(" TryLoginCheck ","Here");
HttpURLConnection connection;
OutputStreamWriter request = null;
URL url = null;
String response = null;
String temp=null;
String parameters = "username="+mUsername+"&password="+mPassword;
System.out.println("UserName"+mUsername+"\n"+"password"+mPassword);
Log.d("Parameters",parameters);
try
{
;
linkurl = new Koneksi(this);
SERVER_URL = linkurl.getUrl();
SERVER_URL += "/mobile/Login.php";
url = new URL(SERVER_URL);
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestMethod("POST");
request = new OutputStreamWriter(connection.getOutputStream());
request.write(parameters);
request.flush();
request.close();
String line = "";
InputStreamReader isr = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
temp=sb.toString();
Log.d("Temp",temp);
response = sb.toString();
Log.d("Response",response);
Log.d("Sb Value",sb.toString());
isr.close();
reader.close();
}
catch(IOException e) {
Toast.makeText(this,e.toString(),Toast.LENGTH_SHORT).show();
}
return response;
}
class LoginTask extends AsyncTask<String, Void, Integer> {
public LoginTask(Login activity, ProgressDialog progressDialog){
}
@Override
protected void onPreExecute(){
progressDialog.show();
}
@Override
protected Integer doInBackground(String... arg0){
}
private void createDialog(String title, String text) {
AlertDialog ad = new AlertDialog.Builder(this)
.setPositiveButton("Ok", null)
.setTitle(title)
.setMessage(text)
.create();
ad.show();
}
}
login.java - edited -> is it like this?
package com.karismaelearning;
public class Login extends Activity {
public Koneksi linkurl;
String SERVER_URL;
private Button login, register, setting;
private EditText username, password;
public ProgressDialog progressDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
setting = (Button)findViewById(R.id.bsetting);
login = (Button) findViewById(R.id.login);
register = (Button) findViewById(R.id.reg);
username = (EditText) findViewById(R.id.uname);
password = (EditText) findViewById(R.id.pass);
setting.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intentSet = new Intent(Login.this, UrlSetting.class);
startActivity(intentSet);
}
});
register.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intentReg = new Intent(Login.this, Register.class);
startActivity(intentReg);
}
});
login.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
new LoginTask().execute();
}
});
}
protected String tryLogin(String mUsername, String mPassword){
Log.d(" TryLoginCheck ","Here");
HttpURLConnection connection;
OutputStreamWriter request = null;
URL url = null;
String response = null;
String temp=null;
String parameters = "username="+mUsername+"&password="+mPassword;
System.out.println("UserName"+mUsername+"\n"+"password"+mPassword);
Log.d("Parameters",parameters);
try{
linkurl = new Koneksi(this);
SERVER_URL = linkurl.getUrl();
SERVER_URL += "/mobile/Login.php";
url = new URL(SERVER_URL);
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestMethod("POST");
request = new OutputStreamWriter(connection.getOutputStream());
request.write(parameters);
request.flush();
request.close();
String line = "";
InputStreamReader isr = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
temp=sb.toString();
Log.d("Temp",temp);
response = sb.toString();
Log.d("Response",response);
Log.d("Sb Value",sb.toString());
isr.close();
reader.close();
}
catch(IOException e) {
Toast.makeText(this,e.toString(),Toast.LENGTH_SHORT).show();
}
return response;
}
public class LoginTask extends AsyncTask<String, Void, String> {
String response = null;
public LoginTask() {
}
@Override
protected void onPreExecute(){
}
@Override
protected String doInBackground(String... arg0) {
String mUsername = username.getText().toString();
String mPassword = password.getText().toString();
response = tryLogin(mUsername, mPassword).trim();
return response;
}
protected void onPostExecute(String result){
super.onPostExecute(result);
Log.d("Check","Here");
Log.d("Response",response);
if(response.toLowerCase().contains("berhasil")){
String nama = username.getText().toString();
Intent newIntent = new Intent(Login.this, MainPage.class);
Bundle bundle = new Bundle();
bundle.putString("nama", nama);
newIntent.putExtras(bundle);
startActivityForResult(newIntent, 0);
}
else{
//Optional
//Kalau bisa dibuat constant untuk menghindari salah penulisan
String RoleError = "ROLE SALAH";
String UserError = "USER SALAH";
createDialog("Maaf", response.equals(RoleError) ? "Role Anda bukan Student!" : "Username Atau Password Salah!");
}
}
}
private void createDialog(String title, String text) {
AlertDialog ad = new AlertDialog.Builder(this)
.setPositiveButton("Ok", null)
.setTitle(title)
.setMessage(text)
.create();
ad.show();
}
}
解决方案
Put in your OnClick
method
new LoginTask().execute(stringParam);
to execute your method after clicking element.
这篇关于需要帮助,有混淆的AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文