我正在尝试从正在手机(USB)上测试的android活动与笔记本电脑上Apache Tomcat7中的Servlet进行交互.我正在创建一个按钮.点击哪个我应该转到下一个“活动”.但是,当我尝试在两者之间联系服务器时,它将停止响应并且不会转到下一页.如果需要此信息,我正在使用Ubuntu作为我的笔记本电脑 *** 作系统,并在OnePlus中作为我的手机.
以下是Android活动的代码:
protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_intent); button submit = (button) findVIEwByID(R.ID.button1); submit.setonClickListener(new VIEw.OnClickListener() { public voID onClick(VIEw arg0) { new Login1().execute(); } });}private class Login1 extends AsyncTask<String, String, String> { @OverrIDe protected String doInBackground(String... paramss) { DefaulthttpClIEnt httpClIEnt = new DefaulthttpClIEnt(); String url = "http://192.168.1.100:8080/Server1/Server"; List<nameValuePair> params = new ArrayList<nameValuePair>(); params.add(new BasicnameValuePair("u", "username")); params.add(new BasicnameValuePair("p", "pass")); httpPost httpPost = new httpPost(url); Log.i("httpPost","success"); try { httpPost.setEntity(new UrlEncodedFormEntity(params)); Log.i("setEntity", "success"); httpResponse httpResponse = httpClIEnt.execute(httpPost); Log.i("execute", "success"); Intent login = new Intent(getApplicationContext(), act2.class); startActivity(login); } catch (ClIEntProtocolException e) { e.printstacktrace(); Log.i("Exception","ClIEntProtocolException"); } catch (UnsupportedEnCodingException e) { e.printstacktrace(); Log.i("Exception", "UnsupportedEnCodingException"); } catch (IOException e) { e.printstacktrace(); Log.i("Exception", "IOException"); } return null; }}
以下是我在servlet上的doPost方法.
protected voID doPost(httpServletRequest request, httpServletResponse response) throws servletexception, IOException { // Todo auto-generated method stub String name=request.getParameter("u").toString(); String password=request.getParameter("p").toString(); response.getWriter().write(name+password); //working }
这是我尝试过的方法:我在笔记本电脑上制作了一个本地Java文件.尝试与apache tomcat7联系.它可以联系并获得所需的结果.但是我的androID活动无法与服务器联系.它不会将控件传递给act2活动.
我处于学习阶段,因此我尝试使事情尽可能简单.
附加信息:如果需要,我已连接wifi上网,家庭网络.
logcat输出:
> 05-22 04:42:16.368 21511-23633/com.example.bhavya.sampleintent I/httpPost﹕ success05-22 04:42:16.369 21511-23633/com.example.bhavya.sampleintent I/setEntity﹕ success05-22 04:43:19.575 21511-23633/com.example.bhavya.sampleintent W/System.err﹕ org.apache.http.conn.httpHostConnectException: Connection to http://192.168.1.100:8080 refused05-22 04:43:19.576 21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at org.apache.http.impl.conn.DefaultClIEntConnectionoperator.openConnection(DefaultClIEntConnectionoperator.java:183)05-22 04:43:19.576 21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)05-22 04:43:19.576 21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)05-22 04:43:19.576 21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at org.apache.http.impl.clIEnt.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)05-22 04:43:19.576 21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at org.apache.http.impl.clIEnt.AbstracthttpClIEnt.execute(AbstracthttpClIEnt.java:555)05-22 04:43:19.577 21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at org.apache.http.impl.clIEnt.AbstracthttpClIEnt.execute(AbstracthttpClIEnt.java:487)05-22 04:43:19.577 21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at org.apache.http.impl.clIEnt.AbstracthttpClIEnt.execute(AbstracthttpClIEnt.java:465)05-22 04:43:19.577 21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at com.example.bhavya.sampleintent.IntentAct$Login1.doInBackground(IntentAct.java:117)05-22 04:43:19.577 21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at com.example.bhavya.sampleintent.IntentAct$Login1.doInBackground(IntentAct.java:99)05-22 04:43:19.577 21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at androID.os.AsyncTask.call(AsyncTask.java:288)05-22 04:43:19.577 21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)05-22 04:43:19.577 21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at androID.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)05-22 04:43:19.577 21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)05-22 04:43:19.577 21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)05-22 04:43:19.577 21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at java.lang.Thread.run(Thread.java:818)05-22 04:43:19.577 21511-23633/com.example.bhavya.sampleintent W/System.err﹕ Caused by: java.net.ConnectException: Failed to connect to /192.168.1.100 (port 8080): connect Failed: ETIMEDOUT (Connection timed out)05-22 04:43:19.577 21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at libcore.io.IoBrIDge.connect(IoBrIDge.java:124)05-22 04:43:19.577 21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)05-22 04:43:19.577 21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)05-22 04:43:19.578 21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at java.net.socket.connect(Socket.java:882)05-22 04:43:19.578 21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)05-22 04:43:19.578 21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at org.apache.http.impl.conn.DefaultClIEntConnectionoperator.openConnection(DefaultClIEntConnectionoperator.java:144)05-22 04:43:19.578 21511-23633/com.example.bhavya.sampleintent W/System.err﹕ ... 14 more05-22 04:43:19.578 21511-23633/com.example.bhavya.sampleintent W/System.err﹕ Caused by: androID.system.ErrnoException: connect Failed: ETIMEDOUT (Connection timed out)05-22 04:43:19.578 21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at libcore.io.Posix.connect(Native Method)05-22 04:43:19.578 21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:111)05-22 04:43:19.578 21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at libcore.io.IoBrIDge.connectErrno(IoBrIDge.java:137)05-22 04:43:19.578 21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at libcore.io.IoBrIDge.connect(IoBrIDge.java:122)05-22 04:43:19.578 21511-23633/com.example.bhavya.sampleintent W/System.err﹕ ... 19 more05-22 04:43:19.578 21511-23633/com.example.bhavya.sampleintent I/Exception﹕ IOException
我尝试使用本地移动热点网络,将其与Internet断开连接.但这行不通.
注意:我不在模拟器上工作.我正在进行USB调试.
解决方法:
因此,通过您的计算机,您可以访问本地主机,但无法从androID设备访问同一主机.
这意味着您的AndroID设备使用2G / 3G或WIFI连接来访问本地主机.如果那时使用本地主机进行连接,那么您必须位于同一网络中,那么只有您可以访问本地主机.
但是在仿真器中,您可以在同一网络(可能是本地主机网络)中运行,这就是为什么.
最后,可能的解决方案是
>在您的androID设备中使用相同的网络(本地主机网络)
也.手段与WiFi连接到相同的本地主机互联网
连接.这次2G / 3G连接不起作用,因为它们不起作用
在同一网络中
>将构建部分部署到外部,并访问外部IP
连接地址.这次2G / 3G或WIFI都可以使用
以上是内存溢出为你收集整理的我正在尝试通过手机上的android活动与笔记本电脑上的servlet进行交互全部内容,希望文章能够帮你解决我正在尝试通过手机上的android活动与笔记本电脑上的servlet进行交互所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)