使用Android连接到本地服务器
我声明我不是Android编程的新手 我无法连接到服务器,代码错误: 找到所需的java.net.Socket java.lang.Object 这是代码,您可以了解如何操作: 然后我按下连接按钮:使用Android连接到本地服务器,android,mysql,sockets,server,Android,Mysql,Sockets,Server,我声明我不是Android编程的新手 我无法连接到服务器,代码错误: 找到所需的java.net.Socket java.lang.Object 这是代码,您可以了解如何操作: 然后我按下连接按钮: private void RegistraTerminale() { new AlertDialogWrapper.Builder(this) .setTitle(R.string.configurazioni_dialog_registra_titolo)
private void RegistraTerminale() {
new AlertDialogWrapper.Builder(this)
.setTitle(R.string.configurazioni_dialog_registra_titolo)
.setMessage(R.string.configurazioni_dialog_registra_messaggio)
.setPositiveButton(R.string.configurazioni_dialog_registra_si, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
pd = new MaterialDialog.Builder(context)
.title(R.string.configurazioni_progress_dialog_registra_titolo)
.content(R.string.configurazioni_progress_dialog_registra_content)
.progress(true, 0)
.show();
new SincTask().execute(new String[]{_androidId});
}
})
.setNegativeButton(R.string.configurazioni_dialog_registra_no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.show();
}
通过同意启动SincTask类:
private class SincTask extends AsyncTask<String, String, genericresult> {
private SincTask() {
}
protected genericresult doInBackground(String... params) {
genericresult Ret = new genericresult(0, null, BuildConfig.FLAVOR);
try {
String datiXml = params[0].replace('[', ' ').replace(']', ' ').trim();
String ipServer = BuildConfig.FLAVOR;
int PortaServer = 0;
Cursor c = ConnessioneAlServer.getAllServer(db);
while (c.moveToNext()) {
ipServer = c.getString(0);
PortaServer = c.getInt(1);
}
if (ipServer == BuildConfig.FLAVOR || PortaServer == 0) {
ipServer = "192.168.1.2";
PortaServer = 4444;
}
publishProgress(new String[]{"Comunicazione con il server..."});
networkresult r = SendCmd(datiXml, ipServer, PortaServer);
Ret.result = r.result;
if (r.result != 0) {
Ret.errMesg = r.errMesg;
}
} catch (Exception ex) {
Log.e("StartDbSincTask:", ex.getMessage());
}
return Ret;
}
protected void onProgressUpdate(String... values) {
try {
pd.setContent(values[0]);
} catch (Exception ex) {
Log.e("onProgressUpdate:", ex.getMessage());
}
}
protected void onPostExecute(genericresult result) {
try {
if (result.result != 0) {
crea_snackbar("result !=0 " + result.errMesg, true);
} else if (result.errMesg.compareTo(BuildConfig.FLAVOR) != 0) {
crea_snackbar("Errore " + result.errMesg, true);
} else {
crea_snackbar("il terminale è stato correttamente registrato.", true);
}
//pd.dismiss();
} catch (Exception e) {
}
}
}
怎么了?或者说缺少什么
TLVParser类:
public class TLVParser {
public static String toUTF8(String isoString) {
String utf8String = isoString;
if (isoString == null || isoString.equals(BuildConfig.FLAVOR)) {
return utf8String;
}
try {
return new String(isoString.getBytes(), "UTF-8");
} catch (UnsupportedEncodingException e) {
System.out.println("UnsupportedEncodingException is: " + e.getMessage());
return isoString;
}
}
public static final byte[] intToByteArray(int value) {
return new byte[]{(byte) (value >>> 24), (byte) (value >>> 16), (byte) (value >>> 8), (byte) value};
}
public static void reverse(byte[] array) {
if (array != null) {
int j = array.length - 1;
for (int i = 0; j > i; i++) {
byte tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
}
}
}
public static byte[] readTLV(InputStream tlv, int tag) {
if (tlv == null) {
throw new IllegalArgumentException("Invalid TLV");
}
byte[] vals = null;
try {
int c = tlv.read();
if (c != 1 && c == tag) {
ByteBuffer bb = ByteBuffer.allocate(4);
c = tlv.read();
if (c != -1) {
bb.put((byte) c);
}
c = tlv.read();
if (c != -1) {
bb.put((byte) c);
}
c = tlv.read();
if (c != -1) {
bb.put((byte) c);
}
c = tlv.read();
if (c != -1) {
bb.put((byte) c);
}
int Len = bb.getInt(0);
vals = new byte[Len];
int Residuo = Len;
int MaxRead = AccessibilityNodeInfoCompat.ACTION_NEXT_HTML_ELEMENT;
if (AccessibilityNodeInfoCompat.ACTION_NEXT_HTML_ELEMENT > Residuo) {
MaxRead = Residuo;
}
int offSet = 0;
byte[] bytes = new byte[Len];
while (Residuo > 0) {
while (true) {
int len = tlv.read(bytes, 0, MaxRead);
if (len > 0) {
System.arraycopy(bytes, 0, vals, offSet, len);
offSet += len;
Residuo -= len;
if (MaxRead > Residuo) {
MaxRead = Residuo;
}
}
}
}
}
} catch (Exception e) {
String errmsg = e.getMessage();
e.printStackTrace();
}
return vals;
}
public static byte[][] readTLV(String tlvHexString, int tag) throws Throwable {
return readTLV(hexStringToByteArray(tlvHexString), tag);
}
public static byte[][] readTLV(byte[] tlv, int tag) throws Throwable {
Throwable th;
if (tlv == null || tlv.length < 1) {
throw new IllegalArgumentException("Invalid TLV");
}
ArrayList al = new ArrayList();
ByteArrayInputStream is = null;
try {
ByteArrayInputStream is2 = new ByteArrayInputStream(tlv);
while (true) {
try {
int c = is2.read();
if (c == -1) {
break;
} else if (c == tag) {
ByteBuffer bb = ByteBuffer.allocate(4);
c = is2.read();
if (c != -1) {
bb.put((byte) c);
}
c = is2.read();
if (c != -1) {
bb.put((byte) c);
}
c = is2.read();
if (c != -1) {
bb.put((byte) c);
}
c = is2.read();
if (c != -1) {
bb.put((byte) c);
}
int Len = bb.getInt(0);
byte[] value = new byte[Len];
is2.read(value, 0, Len);
al.add(value);
}
} catch (Throwable th2) {
th = th2;
is = is2;
}
}
if (is2 != null) {
try {
is2.close();
} catch (IOException e) {
}
}
byte[][] vals = new byte[al.size()][];
al.toArray(vals);
return vals;
} catch (Throwable th3) {
th = th3;
if (is != null) {
try {
is.close();
} catch (IOException e2) {
}
}
throw th;
}
}
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[(len / 2)];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
}
return data;
}
public static int byteArrayToInt(byte[] b, int offset) {
int value = 0;
for (int i = 0; i < 4; i++) {
value += (b[i + offset] & MotionEventCompat.ACTION_MASK) << ((3 - i) * 8);
}
return value;
}
}
您的问题出现在编译时,与服务器或试图连接到服务器无关。为了重现错误,你唯一需要发布的是
public class genericresult extends fresul {
public Object Dati;
public genericresult(int ret, Object dati, String errormsg) {
super(ret, errormsg);
this.Dati = dati;
}
}
然后陈述:
genericresult r = new genericresult(0, null, BuildConfig.FLAVOR);
Socket nsocket = r.Dati;
这将显示错误消息Required java.net.Socket found java.lang.Object
因此,Dati不是套接字实例
还有一件事:
for (int index = 0; index < 5; index++) {
nsocket.connect(sockaddr, timeout);
if (nsocket.isConnected()) {
ret.Dati = nsocket;
break;
}
ret.result = -1;
ret.errMesg = "Errore collegamento con il server";
}
for(int-index=0;index<5;index++){
nsocket.connect(sockaddr,超时);
如果(nsocket.isConnected()){
ret.Dati=nsocket;
打破
}
ret.result=-1;
ret.errMesg=“Errore collegamento con il server”;
}
循环尝试五次是没有意义的。一次就够了。然后设置更长的超时时间
此外,如果第一次尝试超时,您已经设置了一个结果值和一个错误消息值,当第二次连接正常时,您不会删除或更改该值。您的问题是在编译时,与服务器或尝试连接到服务器无关。为了重现错误,你唯一需要发布的是
public class genericresult extends fresul {
public Object Dati;
public genericresult(int ret, Object dati, String errormsg) {
super(ret, errormsg);
this.Dati = dati;
}
}
然后陈述:
genericresult r = new genericresult(0, null, BuildConfig.FLAVOR);
Socket nsocket = r.Dati;
这将显示错误消息Required java.net.Socket found java.lang.Object
因此,Dati不是套接字实例
还有一件事:
for (int index = 0; index < 5; index++) {
nsocket.connect(sockaddr, timeout);
if (nsocket.isConnected()) {
ret.Dati = nsocket;
break;
}
ret.result = -1;
ret.errMesg = "Errore collegamento con il server";
}
for(int-index=0;index<5;index++){
nsocket.connect(sockaddr,超时);
如果(nsocket.isConnected()){
ret.Dati=nsocket;
打破
}
ret.result=-1;
ret.errMesg=“Errore collegamento con il server”;
}
循环尝试五次是没有意义的。一次就够了。然后设置更长的超时时间
此外,如果第一次尝试超时,您已经设置了一个结果值和一个错误消息值,当第二次连接正常时,您不会删除或更改该值。您是否在android清单中授予了internet权限?什么是“本地服务器”?如果您的问题只是建立连接,那么为什么要发布所有代码?别指望我们“把它弄出来”!您应该这样做,并告诉我们问题、错误、异常等。“无法连接到服务器,我得到的代码是这个错误:Required java.net.Socket found java.lang.Object”。这看起来更像是编译时错误。这与无法建立连接无关。你的应用程序甚至不存在或不运行。@greenapps没错,因为这个错误,我甚至无法填写应用程序。我只包含了在连接过程中调用的代码,因此您可以缺席帮助aggevolati。我在SendCmd方法中收到的错误:nsocket=r.Dati;本地服务器是一个驻留在计算机上的程序,它管理数据和其他所有内容。我声明此代码运行到另一个应用程序中,然后我不知道我能做些什么来解决。是的,您添加了logcat。现在,请首先告诉您得到的错误/异常以及代码行。学习阅读日志。一切都在里面。你在你的安卓清单中有没有授予互联网权限?什么是“本地服务器”?如果您的问题只是建立连接,那么为什么要发布所有代码?别指望我们“把它弄出来”!您应该这样做,并告诉我们问题、错误、异常等。“无法连接到服务器,我得到的代码是这个错误:Required java.net.Socket found java.lang.Object”。这看起来更像是编译时错误。这与无法建立连接无关。你的应用程序甚至不存在或不运行。@greenapps没错,因为这个错误,我甚至无法填写应用程序。我只包含了在连接过程中调用的代码,因此您可以缺席帮助aggevolati。我在SendCmd方法中收到的错误:nsocket=r.Dati;本地服务器是一个驻留在计算机上的程序,它管理数据和其他所有内容。我声明此代码运行到另一个应用程序中,然后我不知道我能做些什么来解决。是的,您添加了logcat。现在,请首先告诉您得到的错误/异常以及代码行。学习阅读日志。问题是通过替换socket=r.Dati插入错误来解决的;用nsocket=(Socket)r.Dati;我无法连接,我收到此消息:结果=0错误收集到con il服务器。如何修复?我在问题
Errore collegamento con il server.
上添加了日志。那是你自己的错误。这并没有告诉我们太多。看看你自己的代码。您何时显示错误?你把它放在哪里了?这就是r.result!=0;. 现在返回并找出为什么是0。你可以自己做。建议:不要一直在该字符串中放入相同的错误表,因为这样你就不知道它是哪一个了;用nsocket=(Socket)r.Dati;我无法连接,我收到此消息:结果=0错误收集到con il服务器。如何修复?我在问题Errore collegamento con il server.
上添加了日志。那是你自己的错误。这并没有告诉我们太多。看看你自己的代码。您何时显示错误?你把它放在哪里了?这就是r.result!=0;. 现在返回并找出为什么是0。你可以自己做。建议:不要一直在该字符串中放置相同的错误表
for (int index = 0; index < 5; index++) {
nsocket.connect(sockaddr, timeout);
if (nsocket.isConnected()) {
ret.Dati = nsocket;
break;
}
ret.result = -1;
ret.errMesg = "Errore collegamento con il server";
}