Java 未从Servlet页面获得任何响应

Java 未从Servlet页面获得任何响应,java,android,servlets,Java,Android,Servlets,在Android中,我编写了一个程序,并在一个按钮点击Servlet页面上发出请求,作为响应,它将执行在该Servlet页面上编写的消息,但当我运行项目时,它不会在该按钮点击上执行任何操作 这是我的密码 public static final String URL = "http://10.0.2.2:8080/HttpGetServlet/HelloWorldServlet"; /** Called when the activity is first created. */ @Overri

在Android中,我编写了一个程序,并在一个按钮点击Servlet页面上发出请求,作为响应,它将执行在该Servlet页面上编写的消息,但当我运行项目时,它不会在该按钮点击上执行任何操作

这是我的密码

public static final String URL = "http://10.0.2.2:8080/HttpGetServlet/HelloWorldServlet";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    button = (Button) findViewById(R.id.button);
    outputText = (TextView) findViewById(R.id.outputTxt);

    button.setOnClickListener(this);
}

public void onClick(View view) 
{
    GetXMLTask task = new GetXMLTask();
    task.execute(new String[] { URL });
}

private class GetXMLTask extends AsyncTask<String, Void, String> 
{
    @Override
    protected String doInBackground(String... urls) 
    {
        String output = null;
        for (String url : urls) 
        {
            output = getOutputFromUrl(url);
        }
        return output;
    }

    private String getOutputFromUrl(String url) 
    {
        String output = null;
        try 
        {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            output = EntityUtils.toString(httpEntity);
        } 
        catch (UnsupportedEncodingException e) 
        {
            e.printStackTrace();
        } 
        catch (ClientProtocolException e) 
        {
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
        catch (Exception e)
        {
            Toast.makeText(getApplicationContext(), "Server Not Found!", Toast.LENGTH_LONG).show();
        }
        return output;
    }

    @Override
    protected void onPostExecute(String output) 
    {

        if(output==null)
            Toast.makeText(getApplicationContext(), "Server Not Found!", Toast.LENGTH_LONG).show(); 
        outputText.setText(output);
    }
}
公共静态最终字符串URL=”http://10.0.2.2:8080/HttpGetServlet/HelloWorldServlet";
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
按钮=(按钮)findViewById(R.id.button);
outputText=(TextView)findViewById(R.id.outputText);
setOnClickListener(此);
}
公共void onClick(视图)
{
GetXMLTask task=新建GetXMLTask();
执行(新字符串[]{URL});
}
私有类GetXMLTask扩展了AsyncTask
{
@凌驾
受保护的字符串doInBackground(字符串…URL)
{
字符串输出=null;
for(字符串url:url)
{
输出=getOutputFromUrl(url);
}
返回输出;
}
私有字符串getOutputFromUrl(字符串url)
{
字符串输出=null;
尝试
{
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpGet HttpGet=新的HttpGet(url);
HttpResponse HttpResponse=httpClient.execute(httpGet);
HttpEntity HttpEntity=httpResponse.getEntity();
输出=EntityUtils.toString(httpEntity);
} 
捕获(不支持的编码异常e)
{
e、 printStackTrace();
} 
捕获(客户端协议例外e)
{
e、 printStackTrace();
} 
捕获(IOE异常)
{
e、 printStackTrace();
}
捕获(例外e)
{
Toast.makeText(getApplicationContext(),“找不到服务器!”,Toast.LENGTH\u LONG.show();
}
返回输出;
}
@凌驾
受保护的void onPostExecute(字符串输出)
{
if(输出==null)
Toast.makeText(getApplicationContext(),“找不到服务器!”,Toast.LENGTH\u LONG.show();
outputText.setText(输出);
}
}
若它找不到url,那个么它应该生成一个异常,但我正在捕获异常,那个么也并没有发生任何事情

我错在哪里

请帮忙

谢谢

       try 
            {
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();

                output = EntityUtils.toString(httpEntity);

                System.out.println(output); // here you can check the value of the output String.
            } 
如果它有一个值(它应该),那么问题就出在用户界面上


也许您需要配置接口获取响应的方式。

您是否获取了服务器未找到Toast消息?@Hardik:不,这就是我说的,按钮单击时没有发生任何事情:(确定,然后从servlet返回消息!将这一行放在第一行System.out.println的onPostExcecute方法上(“这是响应”+输出);并查看logcatput您的toast消息在所有异常中,因为在异常情况下,顺序很重要,如果IO异常发生,它不会进入异常块。