我被困在应用程序的注册页面。我已经在 AVD 上进行了测试,它运行良好,但需要注意的是,logcat 受到系统日志的严重攻击。
现在的场景是,当我在手机上测试我的应用程序时,登录 Activity 工作得很好,但是当我进入注册页面时, Activity 卡住了,除了自己强制关闭它或等待我的手机询问我是否要强制关闭它之外,我别无选择。这是我的注册页面的代码。
Activity 名称为Enroll.java
package com.example.dell.iblood;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class enroll extends ActionBarActivity implements View.OnClickListener,AdapterView.OnItemSelectedListener {
EditText Name, Passwordone, Passwordtwo, Age, Address,Phone, Email;
Button REGISTER, RESET;
Spinner States,City,Bloodgroup;
String val = "";
InputStream is = null;
String ip = "http://iblood.byethost3.com/spdemo.php";
String line = null;
String result = null;
List<String> list,list1;
String name;
String paso;
String past;
String age;
String bgroup;
int code;
String add;
String state;
String city;
String phone;
String email;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enroll);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
REGISTER = (Button) findViewById(R.id.Regbutton);
RESET = (Button) findViewById(R.id.Resbutton);
Name = (EditText) findViewById(R.id.nameU);
Passwordone = (EditText) findViewById(R.id.passUone);
Passwordtwo = (EditText) findViewById(R.id.passUtwo);
Age = (EditText) findViewById(R.id.ageU);
Bloodgroup = (Spinner) findViewById(R.id.bgroupU);
Address = (EditText) findViewById(R.id.addressU);
States = (Spinner) findViewById(R.id.stateU);
City = (Spinner) findViewById(R.id.cityU);
States = (Spinner) findViewById(R.id.stateU);
Phone = (EditText) findViewById(R.id.mobU);
Email = (EditText) findViewById(R.id.mailU);
Bloodgroup.setOnItemSelectedListener(this);
States.setOnItemSelectedListener(this);
City.setOnItemSelectedListener(this);
Selectall();
ArrayAdapter<String> adp = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list); // set list into ArrayAdapter
States.setAdapter(adp); // Set
SelectBg();
ArrayAdapter<String> adp3 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list); // set list into ArrayAdapter
Bloodgroup.setAdapter(adp3); // Set
REGISTER.setOnClickListener(this);
RESET.setOnClickListener(this);
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
val = States.getSelectedItem().toString();
selectcity();
ArrayAdapter<String> adp1 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list1); // set list into ArrayAdapter
City.setAdapter(adp1);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
private void Selectall() {
// TODO Auto-generated method stub
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(ip);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("Webservice 1", e.toString());
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("Webservice 2", e.toString());
}
try {
JSONArray ja = new JSONArray(result);
JSONObject jo = null;
list = new ArrayList<String>();
for (int i = 0; i < ja.length(); i++) {
jo = ja.getJSONObject(i);
list.add(jo.getString("nameofstate"));
}
} catch (Exception e) {
Log.e("Webservice 3", e.toString());
}
}
private void selectcity()
{
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("city",val));
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://iblood.byethost3.com/cities.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("pass 1", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address",
Toast.LENGTH_LONG).show();
}
try
{
BufferedReader reader = new BufferedReader
(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.e("pass 2", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 2", e.toString());
}
try {
JSONArray ja = new JSONArray(result);
JSONObject jo = null;
list1 = new ArrayList<String>();
for (int i = 0; i < ja.length(); i++) {
jo = ja.getJSONObject(i);
list1.add(jo.getString("cityname"));
}
} catch (Exception e) {
Log.e("Webservice 13", e.toString());
}
}
private void SelectBg() {
// TODO Auto-generated method stub
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://iblood.byethost3.com/bloodgroup.php");
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("Webservice 1", e.toString());
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("Webservice 2", e.toString());
}
try {
JSONArray ja = new JSONArray(result);
JSONObject jo = null;
list = new ArrayList<String>();
for (int i = 0; i < ja.length(); i++) {
jo = ja.getJSONObject(i);
list.add(jo.getString("Bloodgroup"));
}
} catch (Exception e) {
Log.e("Webservice 3", e.toString());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_enroll, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View view) {
int i = view.getId();
name = Name.getText().toString();
paso = Passwordone.getText().toString();
past = Passwordtwo.getText().toString();
age = Age.getText().toString();
bgroup=Bloodgroup.getSelectedItem().toString();
add = Address.getText().toString();
state= States.getSelectedItem().toString();
city=City.getSelectedItem().toString();
phone = Phone.getText().toString();
email = Email.getText().toString();
switch (i) {
case R.id.Regbutton:
insert();
break;
case R.id.Resbutton:
Name.setText(" ");
Passwordone.setText("");
Passwordtwo.setText("");
Age.setText(" ");
Bloodgroup.setSelection(0);
Address.setText(" ");
States.setSelection(0);
City.setSelection(0);
Phone.setText(" ");
Email.setText(" ");
break;
}
}
public void insert() {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("nameU", name));
nameValuePairs.add(new BasicNameValuePair("passUone", paso));
nameValuePairs.add(new BasicNameValuePair("passUtwo", past));
nameValuePairs.add(new BasicNameValuePair("ageU", age));
nameValuePairs.add(new BasicNameValuePair("bloodgroup",bgroup ));
nameValuePairs.add(new BasicNameValuePair("addressU", add));
nameValuePairs.add(new BasicNameValuePair("nameofstate", state));
nameValuePairs.add(new BasicNameValuePair("cityname", city));
nameValuePairs.add(new BasicNameValuePair("mobU", phone));
nameValuePairs.add(new BasicNameValuePair("mailU", email));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://iblood.byethost3.com/reg.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("pass 11", "connection success ");
} catch (Exception e) {
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address"+e,
Toast.LENGTH_LONG).show();
}
try {
BufferedReader reader = new BufferedReader
(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.e("pass 12", "connection success ");
} catch (Exception e) {
Log.e("Fail 2", e.toString());
}
try
{
JSONObject json_data = new JSONObject(result);
code=(json_data.getInt("code"));
if (code == 1) {
Intent l = new Intent(this, Mainpage.class);
startActivity(l);
Toast.makeText(getBaseContext(), "Registered Successfully \n Please Login",
Toast.LENGTH_SHORT).show();
}
else {
Intent la = new Intent(this, Mainpage.class);
startActivity(la);
Toast.makeText(getBaseContext(), "Sorry, Try Again",
Toast.LENGTH_LONG).show();
}
}
catch (Exception e) {
Log.e("Fail 3", e.toString());
}
}
}
最佳答案
正如 @TomRichardson 所提到的,您的基本问题是您在 Activity 的 UI 线程上执行耗时的操作。
与大多数现代环境一样,您的 Activity 的“主”线程应该始终可用于处理用户输入,使您的程序具有响应能力。如果您想做一些耗时的事情(例如发出 HTTP 请求),您需要在不同的后台线程中执行该操作。
通常这个序列是这样的: 1. UI 线程弹出一个进度微调器以显示其正在工作并启动任务线程。 2. 任务线程工作并在完成时向 UIthread 发出信号 3. UI 线程清除进度微调器并执行接下来需要的任何操作。
如果这一切听起来有点复杂,那么请尝试阅读 Android 的 ASyncTask 类,它以直接且易于理解的方式为您实现了大部分这些内容。
ASyncTask 存在严重问题,随着您对 Android 的了解不断加深,您可能会考虑放弃它,但它是开始执行像您这样的任务的一个很好的简单位置。
关于java - 手机上的注册 Activity 卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30260806/