package com.jqyd.son;
import org.json.JSONException;
import org.json.JSONObject;
import com.jqyd.app.MyApp;
import com.jqyd.manager.R;
import com.jqyd.model.MyLogModule;
import com.jqyd.shareInterface.Optdb_interfce;
import com.jqyd.shareInterface.Optsharepre_interface;
import com.jqyd.shareInterface.UpdataToServer;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Qygg extends Activity implements OnClickListener {
private MyApp myApp;
private EditText xxbt;
private EditText xxnr;
private Button up;
private Button back;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.qygg);
xxbt = (EditText) this.findViewById(R.id.xxbt);
xxnr = (EditText) this.findViewById(R.id.xxnr);
up = (Button) this.findViewById(R.id.up);
back = (Button) this.findViewById(R.id.back);
myApp = (MyApp) this.getApplication();
up.setOnClickListener(this);
back.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v == up){
String xxbtStr = xxbt.getText().toString();
String xxnrStr = xxnr.getText().toString();
if(xxbtStr.equals("") || xxnrStr.equals("")){
warnTell();
}else{
Optsharepre_interface obj_share = new Optsharepre_interface(Qygg.this);
new QyggThread(xxbtStr,xxnrStr,obj_share.getDataFromPres("GGUID"),obj_share.getDataFromPres("GUID"),obj_share.getDataFromPres("GID")).start();
}
}else if(v == back){
finish();
}
}
/**
* 企业公告上传线程
* @author Administrator
*
*/
class QyggThread extends Thread {
private String xxbt;
private String xxnr;
private String guid;
private String zguid;
private String gid;
public QyggThread(String xxbt,String xxnr,String guid,String zguid,String gid) {
this.xxbt = xxbt;
this.xxnr = xxnr;
this.guid = guid;
this.zguid = zguid;
this.gid = gid;
}
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
Looper.prepare();
Message message = new Message();
message.what = 1;
myHander.sendMessage(message);
Message message2 = new Message();
message2.what = 2;
Bundle bundle = new Bundle();
JSONObject object = new JSONObject();
try {
object.put("xxbt", xxbt);
object.put("xxnr", xxnr);
object.put("guid", guid);
object.put("zguid", zguid);
object.put("gid", gid);
object.put("khlb", myApp.getVersion());
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 请求服务器
String result = new UpdataToServer(Qygg.this).dataToServer("QYGG", object);
if (result != null && !result.equals("-1") && !result.equals("500")) {
try {
JSONObject obj = new JSONObject(result);
String res = obj.getString("result");
if (res.equals("0")) {
bundle.putString("msg", "上传成功");
} else {
String errMsg = obj.getString("detail");
bundle.putString("msg", errMsg);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if(result.equals("-1")){
bundle.putString("msg", "抱歉,网络连接失败!");
}else{
bundle.putString("msg", "上报失败,请联系你的管理员!");
}
message2.setData(bundle);
myHander.sendMessage(message2);
Looper.loop();
}
}
Handler myHander = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch (msg.what) {
case 1:
showDialog(1);
break;
case 2:
removeDialog(1);
Bundle data = msg.getData();
String message = data.getString("msg");
showToast(message);
break;
}
}
};
/**
* 清空历史数据
*/
public void cheanHisdatas(){
xxbt.setText("");
xxnr.setText("");
}
/**
* 警告提示
*/
public void warnTell(){
new AlertDialog.Builder(Qygg.this).setTitle("警告").setMessage("请将信息填写完整!")
.setPositiveButton("确定", null).show();
}
/**
* 结果显示
*
* @param message
*/
public void showToast(final String message) {
new AlertDialog.Builder(Qygg.this).setTitle("提示").setMessage(message)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if(message.contains("成功")){
cheanHisdatas();
}
}
}).show();
}
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
ProgressDialog dialog = new ProgressDialog(Qygg.this);
dialog.setIndeterminate(true);
dialog.setCancelable(false);
switch (id) {
case 1:
dialog.setMessage("正在上传,请稍候……");
break;
}
return dialog;
}
}