安卓NFC应用程序与服务器通信出现问题(急)

leezhiqiangcool 2014-06-14 05:35:25
在NFC_WR程序基础上实现将读取ID上传服务器,通过Webservice查询数据库,返回ID对应的数据。
NFC_WR主程序如下


public class MainActivity extends Activity {
private TextView ifo_NFC;
private TextView info;
private NfcAdapter nfcAdapter;
private String readResult = "";
private PendingIntent pendingIntent;
private IntentFilter[] mFilters;
private String[][] mTechLists;
private boolean isFirst = true;
private Button toWBtn;
private Button toSign;
private IntentFilter ndef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//该方法完成接收到Intent时的初始化工作
init();
}


/**
* 检测工作,判断设备的NFC支持情况
* @return
*/
private Boolean ifNFCUse() {
// TODO Auto-generated method stub
if (nfcAdapter == null) {
ifo_NFC.setText("设备不支持NFC!");
finish();
return false;
}
if (nfcAdapter != null && !nfcAdapter.isEnabled()) {
ifo_NFC.setText("请在系统设置中先启用NFC功能!");
finish();
return false;
}
return true;
}

/**
* 初始化过程
*/
private void init() {
// TODO Auto-generated method stub
toWBtn=(Button)findViewById(R.id.toWBtn);
toSign=(Button)findViewById(R.id.toSign);

toWBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,Write2Nfc.class);
startActivity(intent);
}
});
ifo_NFC = (TextView) findViewById(R.id.ifo_NFC);
info = (TextView) findViewById(R.id.info);
//NFC适配器,所有的关于NFC的操作从该适配器进行
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if(!ifNFCUse()){
return;
}
//将被调用的Intent,用于重复被Intent触发后将要执行的跳转
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
//设定要过滤的标签动作,这里只接收ACTION_NDEF_DISCOVERED类型
ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
ndef.addCategory("*/*");
mFilters = new IntentFilter[] { ndef };// 过滤器
mTechLists = new String[][] { new String[] { NfcA.class.getName() },
new String[] { NfcF.class.getName() },
new String[] { NfcB.class.getName() },
new String[] { NfcV.class.getName() } };// 允许扫描的标签类型

if (isFirst) {
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent()
.getAction())) {
System.out.println(getIntent().getAction());
if (readFromTag(getIntent())) {
ifo_NFC.setText(readResult);
System.out.println("1.5...");
} else {
ifo_NFC.setText("标签数据为空");
}
}
isFirst = false;
}
System.out.println("onCreate...");
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
nfcAdapter.disableForegroundDispatch(this);
System.out.println("onPause...");
}

/*
* 重写onResume回调函数的意义在于处理多次读取NFC标签时的情况
* (non-Javadoc)
* @see android.app.Activity#onResume()
*/
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
// 前台分发系统,这里的作用在于第二次检测NFC标签时该应用有最高的捕获优先权.
nfcAdapter.enableForegroundDispatch(this, pendingIntent, mFilters,
mTechLists);


System.out.println("onResume...");
}

/*
* 有必要要了解onNewIntent回调函数的调用时机
* (non-Javadoc)
* @see android.app.Activity#onNewIntent(android.content.Intent)
*/
@Override
protected void onNewIntent(Intent intent) {
// TODO Auto-generated method stub
super.onNewIntent(intent);
System.out.println("onNewIntent1...");
System.out.println(intent.getAction());
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())||
NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
System.out.println("onNewIntent2...");
if (readFromTag(intent)) {
ifo_NFC.setText(readResult);

String methodName = "selectAllCargoInfor";
ArrayList<String> Parameters = new ArrayList<String>();
Parameters.add("stdXh");
ArrayList<String> ParValues = new ArrayList<String>();
ParValues.add(readResult);

GetStudentInfo studentInfo = new GetStudentInfo();
ArrayList<String> studentInfoList = studentInfo.GetWebServre(methodName, Parameters, ParValues);
ifo_NFC.setText(studentInfoList.toString());

//studentInfo.sendHttpRequest(readResult, ifo_NFC);
//ifo_NFC.setText(studentInfo.sendPost("http://118.228.174.199/W/WebService3/Service1.asmx", readResult));

System.out.println("onNewIntent3...");
} else {
ifo_NFC.setText("标签数据为空");
}
}

}

/**
* 读取NFC标签数据的操作
* @param intent
* @return
*/
private boolean readFromTag(Intent intent) {
Parcelable[] rawArray = intent
.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawArray != null) {
NdefMessage mNdefMsg = (NdefMessage) rawArray[0];
NdefRecord mNdefRecord = mNdefMsg.getRecords()[0];
try {
if (mNdefRecord != null) {
readResult = new String(mNdefRecord.getPayload(), "UTF-8");
return true;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return false;
}
return false;
}


连接服务器端代码:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class GetStudentInfo {
public ArrayList<String> GetWebServre(String methodName, ArrayList<String> Parameters, ArrayList<String> ParValues) {
ArrayList<String> Values = new ArrayList<String>();

//ServerUrl是指webservice的url
//10.0.2.2是让android模拟器访问本地(PC)服务器,不能写成127.0.0.1
//4124是指端口号,即挂载到IIS上的时候开启的端口
//Service1.asmx是指提供服务的页面
String ServerUrl = "http://118.228.174.199/W/WebService3/Service1.asmx";

String soapAction = "http://tempuri.org/" + methodName;
//String data = "";
String soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ "<soap:Body />";
String tps, vps, ts;
String mreakString = "";

mreakString = "<" + methodName + " xmlns=\"http://tempuri.org/\">";
for (int i = 0; i < Parameters.size(); i++) {
tps = Parameters.get(i).toString();
//设置该方法的参数为.net webService中的参数名称
vps = ParValues.get(i).toString();
ts = "<" + tps + ">" + vps + "</" + tps + ">";
mreakString = mreakString + ts;
}
mreakString = mreakString + "</" + methodName + ">";
/*
+"<HelloWorld xmlns=\"http://tempuri.org/\">"
+"<x>string11661</x>"
+"<SF1>string111</SF1>"
+ "</HelloWorld>"
*/
String soap2 = "</soap:Envelope>";
String requestData = soap + mreakString + soap2;
//System.out.println(requestData);

try {
URL url = new URL(ServerUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
byte[] bytes = requestData.getBytes("utf-8");
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setConnectTimeout(6000);// 设置超时时间
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
con.setRequestProperty("SOAPAction", soapAction);
con.setRequestProperty("Content-Length", "" + bytes.length);
OutputStream outStream = con.getOutputStream();
outStream.write(bytes);
outStream.flush();
outStream.close();
InputStream inStream = con.getInputStream();

//data=parser(inStream);
//System.out.print("11");
Values = inputStreamtovaluelist(inStream, methodName);
//System.out.println(Values.size());
return Values;

} catch (Exception e) {
System.out.print("2221");
return null;
}
}

public ArrayList<String> inputStreamtovaluelist(InputStream in, String MonthsName) throws IOException {
StringBuffer out = new StringBuffer();
String s1 = "";
byte[] b = new byte[4096];
ArrayList<String> Values = new ArrayList<String>();
Values.clear();

for (int n; (n = in.read(b)) != -1;) {
s1 = new String(b, 0, n);
out.append(s1);
}

System.out.println(out);
String[] s13 = s1.split("><");
String ifString = MonthsName + "Result";
String TS = "";
String vs = "";

Boolean getValueBoolean = false;
for (int i = 0; i < s13.length; i++) {
TS = s13[i];
System.out.println(TS);
int j, k, l;
j = TS.indexOf(ifString);
k = TS.lastIndexOf(ifString);

if (j >= 0) {
System.out.println(j);
if (getValueBoolean == false) {
getValueBoolean = true;
} else {

}

if ((j >= 0) && (k > j)) {
System.out.println("FFF" + TS.lastIndexOf("/" + ifString));
//System.out.println(TS);
l = ifString.length() + 1;
vs = TS.substring(j + l, k - 2);
//System.out.println("fff"+vs);
Values.add(vs);
System.out.println("退出" + vs);
getValueBoolean = false;
return Values;
}

}
if (TS.lastIndexOf("/" + ifString) >= 0) {
getValueBoolean = false;
return Values;
}
if ((getValueBoolean) && (TS.lastIndexOf("/" + ifString) < 0) && (j < 0)) {
k = TS.length();
//System.out.println(TS);
vs = TS.substring(7, k - 8);
//System.out.println("f"+vs);
Values.add(vs);
}

}

return Values;
}

现在问题是,始终无法与服务器建立连接,请大家帮忙看看这两段程序有什么问题?
或者是相关设置问题?已经配置安卓应用程序访问internet的权限,还有其他需要配置的地方吗?
或者是服务器端的问题,在别的电脑上可以访问Webservice,但在我的手机上却不能。

求大神给予指点!
...全文
233 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

80,492

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧