80,351
社区成员
发帖
与我相关
我的任务
分享
package com.qiao.tiebatime.timeblog;
import com.qiao.tiebatime.model.User;
import android.app.Application;
public class CustomApplication extends Application {
private int id;
private String userName;
private String birth;
private String nick;
private String sex;
private String photo;
private String phone;
public CustomApplication(int id, String userName, String birth,
String nick, String sex, String photo, String phone) {
this.id = id;
this.userName = userName;
this.birth = birth;
this.nick = nick;
this.sex = sex;
this.photo = photo;
this.phone = phone;
}
public CustomApplication(int id, String userName) {
super();
this.id = id;
this.userName = userName;
}
public CustomApplication() {
// TODO Auto-generated constructor stub
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
public String getNick() {
return nick;
}
public void setNick(String nick) {
this.nick = nick;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
id = 0;
userName = null;
birth = null;
nick = null;
sex = null;
photo = null;
phone = null;
super.onCreate();
}
}
package com.qiao.tiebatime.activity;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.qiao.tiebatime.R;
import com.qiao.tiebatime.model.User;
import com.qiao.tiebatime.timeblog.CustomApplication;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
//登陆、注册页面
public class ActivityRegister extends Activity {
private TextView mTextView;
public EditText et1, et2;
Map<String, String> map = new HashMap<String, String>();
public String i;
public String j;
final CustomApplication app = (CustomApplication) getApplication();
User user = new User();
public String zhongjian;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_register);
mTextView = (TextView) this.findViewById(R.id.textView2);
et1 = (EditText) findViewById(R.id.et1);
et2 = (EditText) findViewById(R.id.et2);
// 登陆
Button bt1 = (Button) findViewById(R.id.button1);
bt1.setOnClickListener(new Button.OnClickListener() {
@SuppressLint("ShowToast")
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (et1.getText().toString().equals("")
|| et2.getText().toString().equals("")) {
Toast.makeText(ActivityRegister.this, "请输入用户名和密码!", 1000)
.show();
} else {
j = "http://10.0.2.2:8080/MyBlog/login";
new MyThread().start();
}
}
});
}
// 接收子线程发来的消息
class MyHandler extends Handler {
@SuppressLint("HandlerLeak")
public MyHandler(Looper looper) {
super(looper);
}
@SuppressLint("ShowToast")
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
mTextView.setText((String) msg.obj);
Intent intent = new Intent(ActivityRegister.this,
com.qiao.tiebatime.fragment.FragmentMain.class);
Toast.makeText(ActivityRegister.this, "登录成功", 1000).show();
startActivity(intent);
finish();
}
}
// 执行post请求的子线程
class MyThread extends Thread {
@SuppressLint("ShowToast")
public void run() {
String httpUrl = j;
HttpPost httpRequest = new HttpPost(httpUrl);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", et1.getText().toString()));
params.add(new BasicNameValuePair("psw", et2.getText().toString()));
try {
HttpEntity httpentity = new UrlEncodedFormEntity(params, "utf8");
httpRequest.setEntity(httpentity);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse httpResponse = httpclient.execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String strResult = EntityUtils.toString(httpResponse
.getEntity());
i = strResult;
parseJsonObject2(i);
} else {
mTextView.setText("请求错误!");
}
} catch (ClientProtocolException e) {
// mTextView.setText(e.getMessage().toString());
} catch (IOException e) {
// mTextView.setText(e.getMessage().toString());
} catch (Exception e) {
// mTextView.setText(e.getMessage().toString());
}
Looper looper = Looper.getMainLooper();
MyHandler handler = new MyHandler(looper);
Message message = handler.obtainMessage(1, i);
handler.sendMessage(message);
}
}
// 自己写的乱七八糟的json解析方法2
// @SuppressLint("ShowToast")
public void parseJsonObject2(String json) {
try {
JSONObject jsonObject = new JSONObject(json);
String birth = jsonObject.getString("birth");
String id = jsonObject.getString("id");
String nick = jsonObject.getString("nick");
String phone = jsonObject.getString("phone");
String sex = jsonObject.getString("sex");
String userName = jsonObject.getString("userName");
app.setNick(nick);//不能赋值
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.qiao.tiebatime"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:name="com.qiao.tiebatime.timeblog.CustomApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.qiao.tiebatime.activity.ActivityLogo"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.qiao.tiebatime.activity.ActivityRegister" >
</activity>
<activity android:name="com.qiao.tiebatime.fragment.FragmentMain" >
</activity>
<activity android:name="com.qiao.tiebatime.activity.ActivityIssue" >
</activity>
<activity android:name="com.qiao.tiebatime.timeblog.CustomApplication" >
</activity>
</application>
</manifest>