Android 为什么我从servlet中什么也得不到?

Android 为什么我从servlet中什么也得不到?,android,servlets,android-asynctask,Android,Servlets,Android Asynctask,我正在努力编一本简单的词典。用户输入是英语单词,必须使用servlet将其翻译成法语。代码看起来不错,我的意思是没有错误。但当我运行它时,我什么也得不到! 我怎样才能让它显示翻译的单词?。谢谢-史蒂夫 我的主要活动: public class MainActivityDictionary extends Activity implements View.OnClickListener { HttpClient httpClient; Button btn;

我正在努力编一本简单的词典。用户输入是英语单词,必须使用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);
    }
}

首先,使用浏览器尝试您的请求。你得到正确的回答了吗?在servlet中添加日志记录也是非常有用的——至少要确保您收到了请求,然后深入检查您是否回答了什么。还可以在客户端添加日志记录。我尝试了这个,但得到了一个空的浏览器!。当我使用:,我看到了我的servlet,但无法进一步!。我走得更深了,我自己也看不出哪里出了错。servlet是否收到了正确的请求?它被正确地调用了吗?在寻求帮助之前,你应该先调试一下,就像我说的,我在浏览器上尝试了所有东西,调试。我曾多次尝试对我的servlet设置bug,但我无法解决问题。我为此工作了几个小时,我发布了我的请求,因为我几乎放弃了!您可以在控制台上尝试打印,至少可以查看请求是否正常。您应该尝试打印请求参数。