[英]Not getting any response from Servlet Page
在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,则应该生成一个异常,但我正在捕获异常,那么也没有任何异常。
我错了。
请帮忙。
谢谢。
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.
}
如果它有一个值(应该),那么您的问题出在您的用户界面上。
也许您需要配置界面获取响应的方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.