我在尝试将AsyncTask
应用于现有代码时遇到了困难。
首先,我说我的代码工作正常,直到我提升目标SDK,然后收到以下错误消息:
android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
经过一番研究,看起来这是因为我试图在主线程上运行网络操作,这是一个很大的禁忌。解决此问题的方法是使用Asynch任务来运行网络操作。
好的,到目前为止对我来说很有意义,现在我所要做的就是以某种方式在代码中实现asynch任务,这就是我的问题。
基本上,我有一个登录屏幕,可以在成功登录时进入主页(有点像Facebook)。当您单击登录按钮时,它会向我的服务器上的PHP文件发送HTTP请求,该文件验证登录名/密码并发回响应。根据该响应,它会将您登录(或者为您提供“无效登录”响应“)。
所以我很确定这是罪魁祸首。现在,我的问题是,如何在Asynch中运行此任务?我不是在寻找任何人来编写我的代码或任何东西,我只是在寻找一些如何开始这个的指导。我已经在这里呆了几天了,我很紧张:(。
以下是登录类的代码,您可以在底部看到我的Asynch:
public class AndroidLogin extends Activity implements OnClickListener {
Button ok,back,exit;
TextView result;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Login button clicked
ok = (Button)findViewById(R.id.btn_login);
ok.setOnClickListener(this);
result = (TextView)findViewById(R.id.tbl_result);
}
public void postLoginData() {
// Add user name and password
EditText uname = (EditText)findViewById(R.id.txt_username);
String username = uname.getText().toString();
EditText pword = (EditText)findViewById(R.id.txt_password);
String password = pword.getText().toString();
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
// login.php returns true if username and password match in db
HttpPost httppost = new HttpPost("http://www.alkouri.com/android/login.php?username=" + username + "&password=" + password );
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
Log.w("SENCIDE", "Execute HTTP Post Request");
HttpResponse response = httpclient.execute(httppost);
String str = inputStreamToString(response.getEntity().getContent()).toString();
Log.w("SENCIDE", str);
if(str.toString().equalsIgnoreCase("true"))
{
Log.w("SENCIDE", "TRUE");
result.setText("Login Successful! Please Wait...");
}else
{
Log.w("SENCIDE", "FALSE");
result.setText(str);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private StringBuilder inputStreamToString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
// Read response until the end
try {
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
// Return full string
return total;
}
//when register button is clicked
public void RegisterButton(View view) {
Intent myIntent = new Intent(AndroidLogin.this, Registration.class);
AndroidLogin.this.startActivity(myIntent);
}
protected void onPostExecute(Void v){
// turns the text in the textview "Tbl_result" into a text string called "tblresult"
TextView tblresult = (TextView) findViewById(R.id.tbl_result);
// If "tblresult" text string matches the string "Login Successful! Please Wait..." exactly, it will switch to next activity
if (tblresult.getText().toString().equals("Login Successful! Please Wait...")) {
Intent intent = new Intent();
//take text in the username/password text boxes and put them into an extra and push to next activity
EditText uname2 = (EditText)findViewById(R.id.txt_username);
String username2 = uname2.getText().toString();
EditText pword2 = (EditText)findViewById(R.id.txt_password);
String password2 = pword2.getText().toString();
intent.putExtra("username2", username2 + "&pword=" + password2);
startActivity(intent);
}
}
public void onClick(View view) {
class PostLogingDataTask extends AsyncTask<Void,Void,Void>
{
protected Void doInBackground (Void... t)
{
postLoginData();
return null;
}
}
new PostLogingDataTask ().execute();
}
}
以下是我遇到的错误:
Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
答案 0 :(得分:0)
public void onClick(View view) {
class PostLogingDataTask extends AsyncTask<Void,Void,Void>
{
protected Void doInBackground (Void... t)
{
postLoginData();
}
protected void onPostExecute(Void v){
// turns the text in the textview "Tbl_result" into a text string called "tblresult"
TextView tblresult = (TextView) findViewById(R.id.tbl_result);
// If "tblresult" text string matches the string "Login Successful! Please Wait..." exactly, it will switch to next activity
if (tblresult.getText().toString().equals("Login Successful! Please Wait...")) {
Intent intent = new Intent(this, Homepage.class);
//take text in the username/password text boxes and put them into an extra and push to next activity
EditText uname2 = (EditText)findViewById(R.id.txt_username);
String username2 = uname2.getText().toString();
EditText pword2 = (EditText)findViewById(R.id.txt_password);
String password2 = pword2.getText().toString();
intent.putExtra("username2", username2 + "&pword=" + password2);
startActivity(intent);
}
}
}
new PostLogingDataTask ().execute();
}
答案 1 :(得分:0)
使用线程执行网络操作并使用处理程序到UI线程
new Thread(new runnable(){
@Override
public void run() {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
Log.w("SENCIDE", "Execute HTTP Post Request");
HttpResponse response = httpclient.execute(httppost);
String str = inputStreamToString(response.getEntity().getContent()).toString();
Log.w("SENCIDE", str);
Message msg = new Message();
Bundle bundle = new Bundle();
bundle.putString("str",str);
msg.setData(bundle);
handler.sendMessage(msg);
}
}).start();
Handler handler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
str = msg.getData().getString("str");
if(str.toString().equalsIgnoreCase("true"))
{
Log.w("SENCIDE", "TRUE");
result.setText("Login Successful! Please Wait...");
}else
{
Log.w("SENCIDE", "FALSE");
result.setText(str);
}
}
};