Android客户端与服务器端的json数据交互,主要是通过json形式的数据交互,就是json的写入和解析。
先看效果图,我最讨厌讲东西,一个图没有的。
算了,看来我不是写博客的材料,写不下去了,要排版之类的麻烦,大家还是直接去下载源码,里面有大量的注视,应该能看懂。 下载地址:源码下载地址
登录界面:很传统的随便做了一下:

用的是android4.0的,别人说这样的EditText很有科技感。
注册界面:

也是很传统的,文本框之类的 。
下面看看包名,类名图。(忙着和比人聊天了,差点忘了) 现在继续
客户端的:
服务器端的:
下面贴上客户端的代码:
LoginRegisterActivity.java
Register.java
-
<span style="font-size:18px;">package com.gem.hsx.activity;
-
-
import java.io.File;
-
import java.io.FileInputStream;
-
import java.io.FileNotFoundException;
-
import java.io.InputStream;
-
import java.util.ArrayList;
-
import java.util.List;
-
-
import com.gem.hsx.bean.User;
-
import com.gem.hsx.json.WriteJson;
-
import com.gem.hsx.operation.Operaton;
-
-
import android.app.Activity;
-
import android.app.AlertDialog;
-
import android.app.Dialog;
-
import android.app.ProgressDialog;
-
import android.content.DialogInterface;
-
import android.content.Intent;
-
import android.graphics.Bitmap;
-
import android.graphics.BitmapFactory;
-
import android.net.Uri;
-
import android.os.Bundle;
-
import android.os.Handler;
-
import android.os.Message;
-
import android.view.View;
-
import android.view.View.OnClickListener;
-
import android.view.View.OnFocusChangeListener;
-
import android.widget.Button;
-
import android.widget.EditText;
-
import android.widget.ImageView;
-
import android.widget.RadioButton;
-
import android.widget.Toast;
-
-
-
public class Register extends Activity {
-
Button submit;
-
Button select;
-
EditText etusername;
-
EditText etpassword;
-
RadioButton ckman;
-
RadioButton ckwoman;
-
EditText etage;
-
ImageView imgphoto;
-
String str;
-
String filepath=null;
-
String jsonString=null;
-
ProgressDialog dialog;
-
private static final int REQUEST_EX = 1;
-
String username=null;
-
String password=null;
-
String sex=null;
-
String age=null;
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.register);
-
init();
-
-
etusername.setOnFocusChangeListener(new EtusernameOnFocusChange());
-
select.setOnClickListener(new SelectOnclick());
-
submit.setOnClickListener(new SubmitOnclick());
-
}
-
private void init()
-
{
-
submit=(Button) findViewById(R.id.submit);
-
select=(Button) findViewById(R.id.select);
-
etusername=(EditText) findViewById(R.id.etusername);
-
etpassword=(EditText) findViewById(R.id.etpassword);
-
ckman=(RadioButton) findViewById(R.id.ckman);
-
ckwoman=(RadioButton) findViewById(R.id.ckwoman);
-
etage=(EditText) findViewById(R.id.etage);
-
imgphoto=(ImageView) findViewById(R.id.imgphoto);
-
dialog=new ProgressDialog(Register.this);
-
dialog.setTitle("上传数据中");
-
dialog.setMessage("请稍等...");
-
}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
private class EtusernameOnFocusChange implements OnFocusChangeListener
-
{
-
public void onFocusChange(View v, boolean hasFocus) {
-
if (!etusername.hasFocus()) {
-
str=etusername.getText().toString().trim();
-
if (str==null||str.length()<=0)
-
{
-
etusername.setError("用户名不能为空");
-
}
-
else
-
{
-
new Thread(new Runnable() {
-
-
-
public void run() {
-
Operaton operaton=new Operaton();
-
String result= operaton.checkusername("Check", str);
-
System.out.println("result:"+result);
-
Message message=new Message();
-
message.obj=result;
-
handler.sendMessage(message);
-
}
-
}).start();
-
}
-
}
-
}
-
}
-
Handler handler=new Handler()
-
{
-
@Override
-
public void handleMessage(Message msg) {
-
String msgobj=msg.obj.toString();
-
System.out.println(msgobj);
-
System.out.println(msgobj.length());
-
-
if (msgobj.equals("t")) {
-
etusername.requestFocus();
-
etusername.setError("用户名"+str+"已存在");
-
}
-
else
-
{
-
etpassword.requestFocus();
-
}
-
super.handleMessage(msg);
-
}
-
};
-
private class SelectOnclick implements OnClickListener
-
{
-
public void onClick(View v) {
-
-
Intent intent = new Intent();
-
intent.putExtra("explorer_title",
-
getString(R.string.dialog_read_from_dir));
-
intent.setDataAndType(Uri.fromFile(new File("/sdcard")), "*/*");
-
intent.setClass(Register.this, ExDialog.class);
-
startActivityForResult(intent, REQUEST_EX);
-
}
-
}
-
protected void onActivityResult(int requestCode, int resultCode,
-
Intent intent) {
-
if (resultCode == RESULT_OK) {
-
-
Uri uri = intent.getData();
-
filepath=uri.toString().substring(6);
-
System.out.println(filepath);
-
-
if(filepath.endsWith("jpg")||filepath.endsWith("png"))
-
{
-
File file=new File(filepath);
-
try {
-
InputStream inputStream=new FileInputStream(file);
-
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
-
imgphoto.setImageBitmap(bitmap);
-
} catch (FileNotFoundException e) {
-
e.printStackTrace();
-
}
-
submit.setClickable(true);
-
}
-
else
-
{
-
submit.setClickable(false);
-
alert();
-
}
-
-
}
-
}
-
-
private class SubmitOnclick implements OnClickListener
-
{
-
public void onClick(View v) {
-
username=etusername.getText().toString().trim();
-
password=etpassword.getText().toString().trim();
-
-
if (ckman.isChecked()) {
-
sex="男";
-
}
-
else {
-
sex="女";
-
}
-
age=etage.getText().toString().trim();
-
if (age==null||age.length()<=0)
-
{
-
etage.requestFocus();
-
etage.setError("年龄不能为空");
-
return ;
-
}
-
-
dialog.show();
-
new Thread(new Runnable() {
-
-
public void run() {
-
-
Operaton operaton=new Operaton();
-
File file=new File(filepath);
-
String photo=operaton.uploadFile(file, "ImgReciver");
-
-
System.out.println("photo---->"+photo);
-
System.out.println("sex:------>"+sex);
-
User user=new User(username, password, sex, age,photo);
-
-
List<User> list=new ArrayList<User>();
-
list.add(user);
-
WriteJson writeJson=new WriteJson();
-
-
jsonString= writeJson.getJsonData(list);
-
System.out.println(jsonString);
-
String result= operaton.UpData("Register", jsonString);
-
Message msg=new Message();
-
System.out.println("result---->"+result);
-
msg.obj=result;
-
handler1.sendMessage(msg);
-
}
-
}).start();
-
-
}
-
}
-
private void alert()
-
{
-
Dialog dialog = new AlertDialog.Builder(this)
-
.setTitle("提示")
-
.setMessage("您选择的不是有效的图片")
-
.setPositiveButton("确定",
-
new DialogInterface.OnClickListener() {
-
public void onClick(DialogInterface dialog,
-
int which) {
-
filepath = null;
-
}
-
})
-
.create();
-
dialog.show();
-
}
-
Handler handler1=new Handler()
-
{
-
@Override
-
public void handleMessage(Message msg) {
-
dialog.dismiss();
-
String msgobj=msg.obj.toString();
-
if(msgobj.equals("t"))
-
{
-
Toast.makeText(Register.this, "注册成功", 0).show();
-
Intent intent=new Intent();
-
intent.setClass(Register.this, LoginRegisterActivity.class);
-
startActivity(intent);
-
}
-
else {
-
Toast.makeText(Register.this, "注册失败", 0).show();
-
}
-
super.handleMessage(msg);
-
}
-
};
-
}</span>
ExDialog.java
-
package com.gem.hsx.activity;
-
-
-
import java.io.File;
-
import java.util.ArrayList;
-
import java.util.HashMap;
-
import java.util.List;
-
import java.util.Map;
-
-
-
import android.app.ListActivity;
-
import android.content.Context;
-
import android.content.Intent;
-
import android.net.Uri;
-
import android.os.Bundle;
-
import android.util.Log;
-
import android.view.Display;
-
import android.view.LayoutInflater;
-
import android.view.View;
-
import android.view.ViewGroup;
-
import android.view.WindowManager;
-
import android.view.WindowManager.LayoutParams;
-
import android.widget.BaseAdapter;
-
import android.widget.ImageView;
-
import android.widget.ListView;
-
import android.widget.TextView;
-
-
-
import com.gem.hsx.activity.R;
-
-
-
public class ExDialog extends ListActivity {
-
private List<Map<String, Object>> mData;
-
private String mDir = "/sdcard";
-
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
-
-
Intent intent = this.getIntent();
-
Bundle bl = intent.getExtras();
-
String title = bl.getString("explorer_title");
-
Uri uri = intent.getData();
-
mDir = uri.getPath();
-
-
-
setTitle(title);
-
mData = getData();
-
MyAdapter adapter = new MyAdapter(this);
-
setListAdapter(adapter);
-
-
-
WindowManager m = getWindowManager();
-
Display d = m.getDefaultDisplay();
-
LayoutParams p = getWindow().getAttributes();
-
p.height = (int) (d.getHeight() * 0.8);
-
p.width = (int) (d.getWidth() * 0.95);
-
getWindow().setAttributes(p);
-
}
-
-
-
private List<Map<String, Object>> getData() {
-
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
-
Map<String, Object> map = null;
-
File f = new File(mDir);
-
File[] files = f.listFiles();
-
-
-
if (!mDir.equals("/sdcard")) {
-
map = new HashMap<String, Object>();
-
map.put("title", "返回上一级目录/");
-
map.put("info", f.getParent());
-
map.put("img", R.drawable.ex_folder);
-
list.add(map);
-
}
-
if (files != null) {
-
for (int i = 0; i < files.length; i++) {
-
map = new HashMap<String, Object>();
-
map.put("title", files[i].getName());
-
map.put("info", files[i].getPath());
-
if (files[i].isDirectory())
-
map.put("img", R.drawable.ex_folder);
-
else
-
map.put("img", R.drawable.ex_doc);
-
list.add(map);
-
}
-
}
-
return list;
-
}
-
-
-
@Override
-
protected void onListItemClick(ListView l, View v, int position, long id) {
-
Log.d("MyListView4-click", (String) mData.get(position).get("info"));
-
if ((Integer) mData.get(position).get("img") == R.drawable.ex_folder) {
-
mDir = (String) mData.get(position).get("info");
-
mData = getData();
-
MyAdapter adapter = new MyAdapter(this);
-
setListAdapter(adapter);
-
} else {
-
finishWithResult((String) mData.get(position).get("info"));
-
}
-
}
-
-
-
public final class ViewHolder {
-
public ImageView img;
-
public TextView title;
-
public TextView info;
-
}
-
-
-
public class MyAdapter extends BaseAdapter {
-
private LayoutInflater mInflater;
-
-
-
public MyAdapter(Context context) {
-
this.mInflater = LayoutInflater.from(context);
-
}
-
-
-
public int getCount() {
-
return mData.size();
-
}
-
-
-
public Object getItem(int arg0) {
-
return null;
-
}
-
-
-
public long getItemId(int arg0) {
-
return 0;
-
}
-
-
-
public View getView(int position, View convertView, ViewGroup parent) {
-
ViewHolder holder = null;
-
if (convertView == null) {
-
holder = new ViewHolder();
-
convertView = mInflater.inflate(R.layout.listview, null);
-
holder.img = (ImageView) convertView.findViewById(R.id.img);
-
holder.title = (TextView) convertView.findViewById(R.id.title);
-
-
convertView.setTag(holder);
-
} else {
-
holder = (ViewHolder) convertView.getTag();
-
}
-
-
-
holder.img.setBackgroundResource((Integer) mData.get(position).get(
-
"img"));
-
holder.title.setText((String) mData.get(position).get("title"));
-
-
return convertView;
-
}
-
}
-
-
-
private void finishWithResult(String path) {
-
Bundle conData = new Bundle();
-
conData.putString("results", "Thanks Thanks");
-
Intent intent = new Intent();
-
intent.putExtras(conData);
-
Uri startDir = Uri.fromFile(new File(path));
-
intent.setDataAndType(startDir,
-
"vnd.android.cursor.dir/lysesoft.andexplorer.file");
-
setResult(RESULT_OK, intent);
-
finish();
-
}
-
};
JsonUtil.java
-
<pre name="code" class="java">package com.gem.hsx.json;
-
-
-
import java.lang.reflect.Type;
-
import java.util.ArrayList;
-
import java.util.List;
-
import com.google.gson.Gson;
-
import com.google.gson.reflect.TypeToken;
-
-
-
-
-
public class JsonUtil {
-
public List<?> StringFromJson (String jsondata)
-
{
-
Type listType = new TypeToken<List<?>>(){}.getType();
-
Gson gson=new Gson();
-
ArrayList<?> list=gson.fromJson(jsondata, listType);
-
return list;
-
-
-
}
-
}</pre><br>
-
<pre></pre>
-
<p></p>
-
WriteJson.java
-
<p><span style="font-size:18px"></span></p>
-
<pre name="code" class="java">package com.gem.hsx.json;
-
-
import java.util.List;
-
import com.google.gson.Gson;
-
-
public class WriteJson {
-
-
-
-
public String getJsonData(List<?> list)
-
{
-
-
Gson gson=new Gson();
-
String jsonstring=gson.toJson(list);
-
return jsonstring;
-
}
-
-
-
-
}</pre><br>
-
<br>
-
<br>
-
<br>
-
<br>
-
<br>
-
<p></p>
-
<br>
-
<pre></pre>
-
<pre></pre>