没有得到Servlet Page的任何回复

时间:2014-01-11 08:15:51

标签: java android servlets

在Android中,我编写了一个程序,并在按钮上发出请求,单击Servlet页面,作为响应,它将执行写在该servlet页面上的消息,但是当我运行我的项目时,它没有对该按钮执行任何操作。 / p>

这是我的代码,

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那么它应该生成一个Exception但是我捕获异常然后也没有什么是happing。

我错了。

请帮忙。

感谢。

1 个答案:

答案 0 :(得分:0)

       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.
            } 

如果它有一个值(它应该),那么你的问题出现在你的用户界面上。

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