块引用
我对Client-Server编程完全陌生,而且我遇到很多问题。 我应该从客户端android应用程序向名为" selector"的java servlet发送一个参数。 servlet使用getParameter方法,获得名称为" selector"的变量。并将一系列JSONObjects发送到Android客户端。 不幸的是,我的应用程序一直在崩溃。
这是我的Servlet代码
public class FeedServlet extends HttpServlet {
public URLGetter urlgetter;
public XMLParser xmlparser;
public URL page_url, page_feed;
public String siteName, selector, siteDescription;
protected void processRequest(HttpServletRequest request, HttpServletResponse response, NodeList nList)
throws ServletException, IOException {
//do nothing
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
selector=request.getParameter("selector");
setWebSiteParam(request,response);//this method reads the selector and
//chooses what to send back to the client
try {
xmlparser=new XMLParser(page_feed);//parses the RSS Feed the client requested
NodeList nodeList=xmlparser.getNodeList();
processRequest(request, response, nodeList);
} catch (ParserConfigurationException | SAXException ex) {
Logger.getLogger(FeedServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response, NodeList nList)
throws ServletException, IOException {
response.setContentType("application/json");
String title,description,link;
JSONObject dataObj=new JSONObject();
try (PrintWriter out = response.getWriter()) {
for (int i = 0; i < nList.getLength(); i++) {
//Parsing the NodeList
Node node=nList.item(i);
Element nodeElement=(Element)node;
title=nodeElement.getElementsByTagName("title").item(0).getTextContent();
description=nodeElement.getElementsByTagName("description").item(0).getTextContent();
link=nodeElement.getElementsByTagName("link").item(0).getTextContent();
description=lengthCorrector(description);
dataObj.put("description", description);
dataObj.put("title", title);
dataObj.put("link",link);
out.println(dataObj);
}
} catch (JSONException ex) {
Logger.getLogger(FeedServletMobile.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
这是我的Android活动代码
public class ShowNews扩展了AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_news);
String buttonId = this.getIntent().getStringExtra("buttonId");
String newsType = this.getIntent().getStringExtra("newsType");
TextView textViewNews = (TextView) findViewById(R.id.textView4);
if (newsType != null) {
textViewNews.setText(newsType);
}
TextView textViewFullcontent=(TextView) findViewById(R.id.textView5);
Downloader download = new Downloader();
download.execute(buttonId);
}
private class Downloader extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String request=params[0];
String serverAddress = null;
String myFeed="I failed";
HttpsURLConnection conn=null;
try {
serverAddress = "http://10.0.2.2:8080/FeedServlet/FeedServletMobile?selector="+
request;
URL url;
url= new URL(serverAddress);
conn= (HttpsURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000 );
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
int response = conn.getResponseCode();
String input;
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((input = br.readLine()) != null){
myFeed+=input;
}
br.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
conn.disconnect();
}
return myFeed;
}
protected void onPostExecute(String result){
TextView textViewFullcontent=(TextView) findViewById(R.id.textView5);
textViewFullcontent.setText(result);
}
}
类下载器包含在Android活动类中,一旦打开此活动,应用程序就会在调用HttpsUrlConnection.open()方法后崩溃。
编辑:URLEncoder.encode方法导致崩溃,但App仍然没有做任何事情。 我将myFeed字符串设置为句子&#34;我失败&#34;:连接超时并且应用程序打印&#34;我失败&#34;在我的textView 中