加黑部分 执行的时候报 IO异常 球大神解答
package com.liu.activity;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
public class LoginActivity extends Activity {
private EditText userName, userPassword;
private CheckBox rem_pw, auto_login;
private Button btn_login;
private ImageButton btnQuit;
private String userNameValue,passwordValue;
private SharedPreferences sp;
ProgressDialog progressDialog;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去除标题
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.login);
//获得实例对象
sp = this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE);
userName = (EditText) findViewById(R.id.et_zh);
userPassword = (EditText) findViewById(R.id.et_mima);
rem_pw = (CheckBox) findViewById(R.id.cb_mima);
auto_login = (CheckBox) findViewById(R.id.cb_auto);
btn_login = (Button) findViewById(R.id.btn_login);
btnQuit = (ImageButton)findViewById(R.id.img_btn);
progressDialog = new ProgressDialog(this);
//判断记住密码多选框的状态
if(sp.getBoolean("ISCHECK", false))
{
//设置默认是记录密码状态
rem_pw.setChecked(true);
userName.setText(sp.getString("USER_NAME", ""));
userPassword.setText(sp.getString("PASSWORD", ""));
//判断自动登陆多选框状态
if(sp.getBoolean("AUTO_ISCHECK", false))
{
//设置默认是自动登录状态
auto_login.setChecked(true);
//跳转界面
Intent intent = new Intent(LoginActivity.this,LogoActivity.class);
LoginActivity.this.startActivity(intent);
}
}
// 登录监听事件 现在默认为用户名为:liu 密码:123
btn_login.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// userNameValue = userName.getText().toString();
// passwordValue = userPassword.getText().toString();
//if(userNameValue.equals("liu")&&passwordValue.equals("123"))
//AT at = new AT();
new AT().execute(userName.getText().toString(),userPassword.getText().toString());
System.out.println();
if("".equals("00"))
{
Toast.makeText(LoginActivity.this,"登录成功", Toast.LENGTH_SHORT).show();
//登录成功和记住密码框为选中状态才保存用户信息
if(rem_pw.isChecked())
{
//记住用户名、密码、
Editor editor = sp.edit();
editor.putString("USER_NAME", userNameValue);
editor.putString("PASSWORD",passwordValue);
editor.commit();
}
//跳转界面
Intent intent = new Intent(LoginActivity.this,LogoActivity.class);
LoginActivity.this.startActivity(intent);
//finish();
}else{
Toast.makeText(LoginActivity.this,"用户名或密码错误,请重新登录", Toast.LENGTH_LONG).show();
}
}
});
//监听记住密码多选框按钮事件
rem_pw.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if (rem_pw.isChecked()) {
System.out.println("记住密码已选中");
sp.edit().putBoolean("ISCHECK", true).commit();
}else {
System.out.println("记住密码没有选中");
sp.edit().putBoolean("ISCHECK", false).commit();
}
}
});
//监听自动登录多选框事件
auto_login.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if (auto_login.isChecked()) {
System.out.println("自动登录已选中");
sp.edit().putBoolean("AUTO_ISCHECK", true).commit();
} else {
System.out.println("自动登录没有选中");
sp.edit().putBoolean("AUTO_ISCHECK", false).commit();
}
}
});
btnQuit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
public class UriAPI {
/** 定义一个Uri **/
public static final String HTTPCustomer ="http://192.168.1.101:8080/JSONDemo/servlet/CustomerServlet";
}
@SuppressWarnings("rawtypes")
class AT extends AsyncTask{
String result="";
@Override
protected void onPreExecute() {
//加载progressDialog
progressDialog.show();
}
@Override
protected Object doInBackground(Object... params_obj) {
CharSequence username="";
CharSequence password="";
username=userName.getText();
password =userPassword.getText();
if(!username.equals("")&&!password.equals("")){
//请求数据
HttpPost httpRequest = new HttpPost(UriAPI.HTTPCustomer);
//创建参数
List<NameValuePair> params=new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username.toString()));
params.add(new BasicNameValuePair("password", password.toString()));
//params.add(new BasicNameValuePair("flag","0"));
try {
//对提交数据进行编码
httpRequest.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
HttpResponse httpResponse=new DefaultHttpClient().execute(httpRequest);
//获取响应服务器的数据
if (httpResponse.getStatusLine().getStatusCode()==200) {
//利用字节数组流和包装的绑定数据
byte[] data =new byte[2048];
//先把从服务端来的数据转化成字节数组
String responseStuts =EntityUtils.toString((HttpEntity)httpResponse.getEntity());
String str="";
try {
JSONArray array = new JSONArray(responseStuts);
result = array.getJSONObject(0).getString("responseStuts");
System.out.println(result);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//再创建字节数组输入流对象
//ByteArrayInputStream bais = new ByteArrayInputStream(data);
//绑定字节流和数据包装流
//DataInputStream dis = new DataInputStream(bais);
//将字节数组中的数据还原成原来的各种数据类型,代码如下:
//result=new String(dis.readUTF());
Log.i("服务器返回信息:", str);
}
} catch(ClientProtocolException e){
e.printStackTrace();
}catch(UnsupportedEncodingException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
@Override
protected void onPostExecute(Object result) {
//获得服务器返回信息成功后
//show_login.setText(result.toString());
//取消进度条
progressDialog.cancel();
}
}
}
浙公网安备 33010602011771号