为什么我的servlet没有任何结果?

时间:2015-03-22 11:38:23

标签: android servlets android-asynctask

我正在尝试制作一本简单的字典。用户输入是英文单词,必须使用servlet将其翻译成法语。代码看起来不错,我的意思是我没有错误。但是当我跑步时我什么都没得到! 如何让它显示翻译的单词?谢谢, - 史蒂夫
我的主要活动:

public class MainActivityDictionary extends Activity implements View.OnClickListener {

        HttpClient httpClient;
        Button btn;
        EditText edEng;
        TextView tv;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main_activity_ord_bok);

            findViewsById();
            btn.setOnClickListener(this);
        }
        public void findViewsById() {
            btn = (Button) findViewById(R.id.btnEngFr);
            edEng = (EditText) findViewById(R.id.txtEng);
            tv = (TextView) findViewById(R.id.txtFr);
        }
        @Override
        public void onClick(View v) {
            switch(v.getId()) {
                case R.id.btnEngFr:
                    doTranslation(); break;
            }
        }
        public void doTranslation() {
            httpClient = new DefaultHttpClient();

            try {
            String eng = edEng.getText().toString();
            //Encodibg String eng
            String urlParams = URLEncoder.encode(eng, "UTF-8");

            new LogInAsyncTaskDictionary(this).execute(new Pair<>(eng, httpClient));
        }
        public void showLoginResult(String result) {
            tv.setText(result);
            edEng.setText("");
        }
    }

我提交表单的servlet以纯文本形式返回结果:

public class DictionaryServlet extends HttpServlet {

    //'myDictionary' map is a class variable, putting  the initialization in a static initializer:
    public static Map<String, String> myDictionary = new HashMap<>();
    static {
        myDictionary.put("car", "voiture");
        myDictionary.put("house", "maison");
        myDictionary.put("screen", "écran");
        myDictionary.put("computer", "ordinateur");
        myDictionary.put("cup", "verre");
        myDictionary.put("mobile phone", "téléphone portable");
    }
    public DictionaryServlet() {
        super();
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        handleRequest(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        handleRequest(request, response);
    }
    private void handleRequest(HttpServletRequest request, HttpServletResponse
            response) throws ServletException, IOException {

        String engWord = request.getParameter("eng");

        if (myDictionary.containsKey(engWord)) {
            String frWord = myDictionary.get(engWord);

            response.setContentType("text/plain");
            response.setContentLength(frWord.length());
            PrintWriter out = response.getWriter();
            out.println(frWord);
        }
    }
}

我正在使用AsyncTask正确使用UI线程:

public class LogInAsyncTaskDictionary extends AsyncTask<Pair<String, HttpClient>, Void, String> {

    private Context context = null;

    public LogInAsyncTaskDictionary(Context context) {
        this.context = context;
    }
    @Override
    protected String doInBackground(Pair<String, HttpClient>... params) {

        Pair pair = params[0];
        String urlParams = (String)pair.first;
        HttpClient httpClient = (HttpClient)pair.second;
        try {
            String serverURL = "http://10.0.2.2:8080/Dictionary?" + urlParams;
            HttpGet httpGet = new HttpGet(serverURL);

            HttpResponse response = httpClient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == 200) {
                return EntityUtils.toString(response.getEntity());
            }
            return "Wrong: " + response.getStatusLine().getStatusCode() + " " + response.getStatusLine().getReasonPhrase();
            //Be sure to catch the ClientProtocolException and IOException thrown.
        } catch (ClientProtocolException e) {
            return e.getMessage();
        } catch (IOException e) {
            return e.getMessage();
        }
    }
    @Override
    protected void onPostExecute(String result) {
        ((MainActivityDictionary)context).showLoginResult(result);
    }
}

0 个答案:

没有答案