我有Android设备三星GT-OS2.2.1
我成功地通过WIFI将HTTP请求发送到本地主机(我的Windows 8 PC)
但是,考虑到速度,我也想学习“如何通过USB发送HTTP请求来本地承载我的windows 8 PC”
这是我的代码,要发送到WIFI
URL url = null;
try {
/*Wireless LAN adapter Local Area Connection*/
url = new URL("http://192.168.xxx.xxx/MySkripsi/testWriteFile.php");
String body = "";
body += "text=" + messageTujuan;
byte[] bytes = body.getBytes();
HttpURLConnection conn = null;
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
OutputStream out = conn.getOutputStream();
out.write(bytes);
out.close();
int status = conn.getResponseCode();
if (status != 200) {
Toast.makeText(con, "status = " + status , 0).show();
}
else
{
InputStream is = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append((line + "\n"));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Toast.makeText(con, "Return Nya = " +sb.toString(), 0).show();
Log.v("TEST" , "Return Nya = " + sb.toString());
tv.setText(sb.toString());
is.close();
}
} catch (Exception e) {
Toast.makeText(this, e.toString() + "#" + e.getMessage(), 0).show();
}这段代码,给我结果
但每当我关掉WIFI它就会显示
"java.net.SocketException:网络不可达“
所以我通过一些网站来研究,我发现了这个
http://www.codeproject.com/Articles/191930/Android-Usb-Port-Forwarding
这是打印屏幕
我的android设备中的USB隧道也说:“服务正在运行,连接!”
但是,使用与上面相同的代码,除了:
url = new URL("h**p://127.0.0.1:80/MySkripsi/testWriteFile.php");结果是"java.netConnectException :/127.0.0.1:80 -连接被拒绝“
我认为我的防火墙有问题,但我不知道如何修复它。
所以我的问题是
还有其他方法可以用于android与服务器之间的通信吗?
我真的需要我的安卓设备发送数据到我的本地主机(Windows8PC)作为我的服务器和接收服务器的响应通过USB
谢谢..。
发布于 2013-09-04 07:19:12
嗯,我开发了一个应用程序来连接我的jsp file.here示例代码。
try
{
URL url = new URL("http:/xx.xxx.xxx.x:80/sample.jsp");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader r = new BufferedReader(new InputStreamReader(in));
String x = "";
String total = "";
int i=0;
ArrayList<String> content = new ArrayList();
while((x = r.readLine()) != null)
{
content.add(x);
}
in.close();
r.close();
}
catch(Exception e)
{
e.printStackTrace();
Toast.makeText(Customer.this, e.toString(), Toast.LENGTH_SHORT).show();
}因此,您可以更改(在url中)这段代码,因为您想在usb中运行wish.If,您必须将三星usb驱动程序下载到您的pc上,然后在设备中运行启用开发人员选项的application.Before。
发布于 2013-09-05 06:11:46
谢谢@Ram和chyrlis
我想分享它,也许它会对其他人有所帮助
所以我搜索了所有的地方,他们都告诉我关于usb拴的事
以下是我为解决自己的问题所做的事情
我的android设备没有以太网
我的android有一个以太网,ip = 192.168.42.201 (注意这个IP是动态的DHCP,所以您也许可以先将它更改为静态的),下面是教程http://www.youtube.com/watch?v=SIYyRYdV7B8
所以我把我的IP改为192.168.42.202
我把网址改为
"url = new URL("http://192.168.42.202/MySkripsi/testWriteFile.php");" 注意,192.168.42.201是我从ipconfig获得的IP,我将我的IP更改为192.168.42.202,因此它将是静态IP。
希望它能帮上忙
https://stackoverflow.com/questions/18607347
复制相似问题