| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1424 人关注过本帖
标题:android客户端访问web服务器问题
只看楼主 加入收藏
wancy
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2016-5-27
结帖率:25%
收藏
 问题点数:0 回复次数:0 
android客户端访问web服务器问题
Android客户端代码:

manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.
    package="com.example.httptest"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.httptest.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>


MainActivity.java代码:
package com.example.httptest;

import
import
import
import
import
import

import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=(Button)findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v)
            {
                sendRequestWithHttpURLConnection();
            }
        });
    }
   
    private void sendRequestWithHttpURLConnection()
    {
        //开启线程来发起网络请求
        new Thread(new Runnable(){
            @Override
            public void run(){
                HttpURLConnection connection =null;
                try {
                    String urlstr="http://192.168.100.100:8080/FirstHttpServletTest/Two";
                    
                    URL url=new URL(urlstr+"?username=wancy");
                    //connection.setDoOutput(true);
                    connection=(HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(8000);
                    InputStream in=connection.getInputStream();
                    //对获取到的输入流进行读取
                    BufferedReader reader=new BufferedReader(new InputStreamReader(in));
                    StringBuilder response=new StringBuilder();
                    String line;
                    while((line=reader.readLine())!=null)
                    {
                        response.append(line);
                    }
                    
                    String show=response.toString();
                    Toast.makeText(MainActivity.this, show, 1).show();//测试时,没有输出此句提示
                } catch (Exception e) {
                    e.printStackTrace();
                }finally{
                    if(connection!=null)
                    {
                        connection.disconnect();
                    }
                }
            }
        }).start();
    }   
}


activity_main.xml代码:
<RelativeLayout xmlns:android="http://schemas.
    xmlns:tools="http://schemas.
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="71dp"
        android:layout_marginTop="101dp"
        android:text="HTTP请求" />  
</RelativeLayout>

服务器端:

Login.java代码:
package com;

import
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//@WebServlet("/Login")
public class Login extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         doPost(request,response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         response.setContentType("text/html;charset=utf-8");
         response.setCharacterEncoding("UTF-8");
         response.getWriter().write("欢迎你:"+request.getParameter("username"));
    }
}

web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www. xmlns="http://java. xmlns:web="http://java. xsi:schemaLocation="http://java. http://java. id="WebApp_ID" version="2.5">
  <display-name>AndroidJSP</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>Login</display-name>
    <servlet-name>Login</servlet-name>
    <servlet-class>com.Login</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Login</servlet-name>
    <url-pattern>/Login</url-pattern>
  </servlet-mapping>
</web-app>
在浏览器中能正常访问服务器,android客户端(wifi,在同一个网段内)访问没反应,求帮助,不甚感激!
2017-02-25 17:36
快速回复:android客户端访问web服务器问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018727 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved