【问题标题】:Not getting any response from Servlet Page没有从 Servlet 页面得到任何响应
【发布时间】:2014-01-30 08:37:51
【问题描述】:

在 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,那么它应该生成一个异常,但我正在捕获异常,那么也没有发生任何事情。

我哪里错了。

请帮忙。

谢谢。

【问题讨论】:

  • 您收到 Server Not Found Toast 消息了吗?
  • @Hardik:不,这就是我的意思,点击按钮时什么都没有发生:(
  • 好的,那么你从 servlet 中返回了一条消息,对吧!?
  • 将此行放在 onPostExcecute 方法的第一行 System.out.println("This is response "+output);并查看 logcat
  • 将您的 toast 消息放入所有异常中,因为在异常情况下,如果 IO 异常发生,则顺序很重要,它不会进入异常块。

标签: java android servlets


【解决方案1】:
       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.
            } 

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

也许您需要配置界面获取响应的方式。

【讨论】:

    猜你喜欢
    • 2018-06-18
    • 2013-05-13
    • 2018-01-21
    • 1970-01-01
    • 2013-01-01
    • 2013-09-05
    • 1970-01-01
    • 2023-01-19
    • 2019-08-31
    相关资源
    最近更新 更多