我被困在我的应用的注册页面上。我已经在AVD上对它进行了测试,但它在那里运行良好,但需要注意的是logcat受到系统日志的猛烈攻击。
现在的情况是,当我在手机上测试我的应用程序时,登录活动工作得很好,但是当我进入注册页面时,活动会冻结&除了强迫自己关闭它或者等我的手机问我是否要强制关闭它我别无选择。这是我的注册页面的代码。
活动名称为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());
}
}
}
答案 0 :(得分:0)
正如@TomRichardson所提到的,你的基本问题是你在活动的UI线程上进行了大量的操作。
与大多数现代环境一样,&#39;主要&#39;您的活动的主题应始终可用于处理用户输入,使您的程序响应。如果你想做一些耗时的事情(例如发出HTTP请求),你需要在不同的后台线程中做这件事。
通常这个序列是这样的: 1. UI线程弹出一个进度微调器以显示它正在工作并启动任务线程。 2.任务线程工作并在完成时发出UIthread信号 3. UI线程清除进度微调器并执行下一步所需的操作。
如果这听起来有点复杂,那么请尝试阅读Android的ASyncTask类,它可以直接,简单地理解您实现大部分这些位。
ASyncTask存在严重问题,随着您对Android的了解,您可能会考虑将其遗忘,但这是一个很容易从像您这样的任务开始的地方。