package com.example.phonefragment;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import org.json.JSONObject;
public class MyFragment extends Fragment {
Handler handler;
String telString;
String nameString;
String tnumString;
String emailString;
Button setmyButton;
TextView tv_username;
TextView tv_name;
TextView tv_tnum;
TextView tv_email;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my, container, false);
handler = new Handler();
setmyButton = (Button) view.findViewById(R.id.btn_fragment_my_setmy);
tv_username = (TextView) view.findViewById(R.id.tv_my_usernamecontent);
tv_name = (TextView) view.findViewById(R.id.tv_my_realnamecontent);
tv_tnum = (TextView) view.findViewById(R.id.tv_my_idcontent);
tv_email = (TextView) view.findViewById(R.id.tv_my_emailcontent);
telString = getActivity().getIntent().getStringExtra("tel");
tv_username.setText(telString);
String url = "http://192.168.1.108:3000/users/query_teacher";
MyFragmentThread httpThread = new MyFragmentThread(url);
httpThread.start();
setmyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(getActivity(), MyActivity.class);
Bundle bundle = new Bundle(); // 创建Bundle对象
bundle.putString("tel", telString); // 装入数据
intent.putExtras(bundle); // 把Bundle塞入Intent里面
startActivityForResult(intent, 1);
}
});
return view;
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
tvShow();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
switch (resultCode) {
case 1:
nameString = data.getStringExtra("name");
tnumString = data.getStringExtra("tnum");
emailString = data.getStringExtra("email");
tvShow();
break;
default:
break;
}
}
class MyFragmentThread extends Thread {
String str;
String url;
public MyFragmentThread(String url) {
// TODO Auto-generated constructor stub
this.url = url;
}
public void doGet() {
HttpClient httpClient = new DefaultHttpClient();
// 注意,下面这一行中,我之前把链接中的"test"误写成了"text",导致调BUG调了半天没弄出来,真是浪费时间啊
String url = this.url + "?tel=" + telString;
// 第二步:创建代表请求的对象,参数是访问的服务器地址
HttpGet httpGet = new HttpGet(url);
try {
// 第三步:执行请求,获取服务器发还的相应对象
HttpResponse response = httpClient.execute(httpGet);
// 第四步:检查相应的状态是否正常:检查状态码的值是200表示正常
if (response.getStatusLine().getStatusCode() == 200) {
// 第五步:从相应对象当中取出数据,放到entity当中
HttpEntity entity = response.getEntity();
BufferedReader reader = new BufferedReader(
new InputStreamReader(entity.getContent()));
StringBuffer sb = new StringBuffer();
while ((str = reader.readLine()) != null) {
sb.append(str);
}
str = sb.toString();
JSONObject resultJsonObject = new JSONObject(str);
nameString = resultJsonObject.getString("name");
tnumString = resultJsonObject.getString("tnum");
emailString = resultJsonObject.getString("email");
tvShow();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
// TODO Auto-generated method stub
doGet();
}
}
public void tvShow() {
tv_name.setText(nameString);
tv_tnum.setText(tnumString);
tv_email.setText(emailString);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/img_my_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:contentDescription="@string/login_in"
android:src="@drawable/user" />
<LinearLayout
android:id="@+id/lin_my_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/img_my_photo"
android:layout_marginTop="40dp"
android:background="#ffffff"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_my_username"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginLeft="14dp"
android:layout_weight="1"
android:background="@null"
android:gravity="left|center_vertical"
android:text="@string/my_username"
android:textColor="@color/black"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_my_usernamecontent"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_weight="1"
android:background="@null"
android:gravity="left|center_vertical"
android:text="@string/no_data"
android:textColor="@color/black"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/myline"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginLeft="14dp"
android:layout_marginRight="14dp"
android:background="#D7D7D7"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="horizontal" >
<TextView
android:id="@+id/edt_my_realname"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginLeft="14dp"
android:layout_weight="1"
android:background="@null"
android:gravity="left|center_vertical"
android:text="@string/my_realname"
android:textColor="@color/black"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_my_realnamecontent"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_weight="1"
android:background="@null"
android:gravity="left|center_vertical"
android:text="@string/no_data"
android:textColor="@color/black"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/mylinetwo"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginLeft="14dp"
android:layout_marginRight="14dp"
android:background="#D7D7D7"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="horizontal" >
<TextView
android:id="@+id/edt_my_id"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginLeft="14dp"
android:layout_weight="1"
android:background="@null"
android:gravity="left|center_vertical"
android:text="@string/my_id"
android:textColor="@color/black"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_my_idcontent"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_weight="1"
android:background="@null"
android:gravity="left|center_vertical"
android:text="@string/no_data"
android:textColor="@color/black"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/mylinethree"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginLeft="14dp"
android:layout_marginRight="14dp"
android:background="#D7D7D7"
android:orientation="vertical" >
</LinearLayout>
<TextView
android:id="@+id/edt_my_award"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginLeft="14dp"
android:background="@null"
android:gravity="left|center_vertical"
android:text="@string/award"
android:textColor="@color/black"
android:textSize="16sp" />
<LinearLayout
android:id="@+id/mylinefour"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginLeft="14dp"
android:layout_marginRight="14dp"
android:background="#D7D7D7"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="horizontal" >
<TextView
android:id="@+id/edt_my_email"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginLeft="14dp"
android:layout_weight="1"
android:background="@null"
android:gravity="left|center_vertical"
android:text="@string/my_email"
android:textColor="@color/black"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_my_emailcontent"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_weight="1"
android:background="@null"
android:gravity="left|center_vertical"
android:text="@string/no_data"
android:textColor="@color/black"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/mylinefive"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginLeft="14dp"
android:layout_marginRight="14dp"
android:background="#D7D7D7"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/btn_fragment_my_setmy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/lin_my_message"
android:layout_marginLeft="15dp"
android:text="@string/set" />
</RelativeLayout>
package com.example.phonefragment;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
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 MyActivity extends Activity {
Handler handler;
EditText setmyname;
EditText setmytnum;
EditText setmyemail;
Button setButton;
TextView myshow;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_my);
handler = new Handler();
setmyname = (EditText) findViewById(R.id.edt_setmyname);
setmytnum = (EditText) findViewById(R.id.edt_setmyid);
setmyemail = (EditText) findViewById(R.id.edt_setmyemail);
setButton = (Button) findViewById(R.id.btn_setmy_confirm);
myshow = (TextView) findViewById(R.id.tv_my_show);
Intent intent = this.getIntent();
Bundle getbundle = intent.getExtras(); // 获取intent里面的bundle对象
final String telString = getbundle.getString("tel"); // 获取Bundle里面的字符串
myshow.setText(telString);
setButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String name = setmyname.getText().toString();
String tnum = setmytnum.getText().toString();
String email = setmyemail.getText().toString();
String url = "http://192.168.1.108:3000/users/update_teacher";
SetMyThread httpThread = new SetMyThread(url, telString, name, tnum,
email);
httpThread.start();
}
});
}
class SetMyThread extends Thread {
String str;
String url;
String tel;
String name;
String tnum;
String email;
public SetMyThread(String url, String tel, String name, String tnum,
String email) {
// TODO Auto-generated constructor stub
this.url = url;
this.tel = tel;
this.name = name;
this.tnum = tnum;
this.email = email;
}
public void doGet() {
HttpClient httpClient = new DefaultHttpClient();
// 注意,下面这一行中,我之前把链接中的"test"误写成了"text",导致调BUG调了半天没弄出来,真是浪费时间啊
String url = this.url + "?tel=" + tel + "&tnum=" + tnum + "&name=" + name + "&email=" + email;
// 第二步:创建代表请求的对象,参数是访问的服务器地址
HttpGet httpGet = new HttpGet(url);
try {
// 第三步:执行请求,获取服务器发还的相应对象
HttpResponse response = httpClient.execute(httpGet);
// 第四步:检查相应的状态是否正常:检查状态码的值是200表示正常
if (response.getStatusLine().getStatusCode() == 200) {
// 第五步:从相应对象当中取出数据,放到entity当中
HttpEntity entity = response.getEntity();
BufferedReader reader = new BufferedReader(
new InputStreamReader(entity.getContent()));
StringBuffer sb = new StringBuffer();
while ((str = reader.readLine()) != null) {
sb.append(str);
}
str = sb.toString();
if (str.trim().equals("empty")) {
// 用户登录失败
handler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(MyActivity.this, "账号不存在",
Toast.LENGTH_LONG).show();
}
});
} else {
if (str.trim().equals("ok")) {
// 用户登录成功
handler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(MyActivity.this, "添加成功",
Toast.LENGTH_LONG).show();
}
});
Intent intent = new Intent(MyActivity.this, MainActivity.class);
/* 将数据打包到aintent Bundle 的过程略 */
Bundle bundle = new Bundle();
bundle.putString("name", this.name);
bundle.putString("tnum", this.tnum);
bundle.putString("email", this.email);
intent.putExtras(bundle); // 把Bundle塞入Intent里面
setResult(1, intent);
MyActivity.this.finish();
} else {
handler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(MyActivity.this, "账号或密码错误",
Toast.LENGTH_LONG).show();
}
});
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
// TODO Auto-generated method stub
doGet();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/edt_setmyname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/my_inputname"
android:ems="10" />
<EditText
android:id="@+id/edt_setmyid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/my_inputid"
android:ems="10" />
<EditText
android:id="@+id/edt_setmyemail"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/my_inputemail"
android:ems="10" />
<TextView
android:id="@+id/tv_my_show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/no_data"/>
<Button
android:id="@+id/btn_setmy_confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="30dp"
android:background="@drawable/btnbg"
android:includeFontPadding="false"
android:text="@string/confirm"
android:textColor="#ffffff" />
</LinearLayout>