与android的Servlet连接

时间:2013-10-09 08:33:26

标签: android servlets

在我的应用程序中,我将数据发送到我的servlet并获得响应。但在一些wifi的工作和在一些wifi它不起作用(gice连接错误)..不知道为什么..这是MyAsyncTask..please帮助我...

   private class MyAsyncTask extends AsyncTask<String, Integer, String>{


        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            String s=postData(params);
            return s;
        }

        protected void onPostExecute(String result){
            pb.setVisibility(View.GONE);
            Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
        }
        protected void onProgressUpdate(Integer... progress){
            pb.setProgress(progress[0]);
        }

        public String postData(String valueIWantToSend[]) {
            // Create a new HttpClient and Post Header



            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://192.168.1.105:8080/ServletParams/AndroidServlet");
            String origresponseText="";
            try {
                // Add your data  cnic,mobileNo,name,address,nextkin
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("param1",valueIWantToSend[0]));
                nameValuePairs.add(new BasicNameValuePair("param1", valueIWantToSend[1]));

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
         /* execute */

                HttpResponse response = httpclient.execute(httppost);
                  HttpEntity rp = response.getEntity();
origresponseText=readContent(response);

            } 
      catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } 
      catch (IOException e) {
                // TODO Auto-generated catch block
            }
            String responseText = origresponseText.substring(7,      origresponseText.length());
            return responseText;

        }

0 个答案:

没有答案