无法从android接收数据?

时间:2012-05-20 22:35:02

标签: android json servlets

我的servlet没有从android客户端接收数据。 logs中没有显示错误或警告。我的android屏幕显示类似于html格式的tomcat错误报告。如何从servlet中检索android中的数据。我的servlet返回null。如何检查我的servlet是否接收数据?

public boolean PostData() 
{
try {
    String URL = "http://10.0.2.2:8080/myapp/StudentDetails"; 
    boolean isPosted;
    String name = getIntent().getExtras().getString("name");
    String classs = getIntent().getExtras().getString("classs");
    String age = getIntent().getExtras().getString("age");

    // creating default Client 
    HttpClient mClient = new DefaultHttpClient();
    // Connect URL 
    StringBuilder sb=new StringBuilder(URL);
    HttpPost mpost = new HttpPost(sb.toString());
    List<BasicNameValuePair> nameValuepairs = new ArrayList<BasicNameValuePair>(3);
    nameValuepairs.add(new BasicNameValuePair("name",name));
    nameValuepairs.add(new BasicNameValuePair("classs",classs));
    nameValuepairs.add(new BasicNameValuePair("age",age));
    mpost.setEntity(new UrlEncodedFormEntity(nameValuepairs));
    HttpResponse responce = mClient.execute(mpost);
    HttpEntity entity = responce.getEntity();
    BufferedReader buf = new BufferedReader(new  InputStreamReader(entity.getContent()));
    StringBuilder sb1 = new StringBuilder();
    String line = null;
    while ((line = buf.readLine()) != null) {
    sb1.append(line+"\n");
    }
    Toast.makeText(getApplicationContext(), sb1.toString()+"",1).show();
    TextView tv = (TextView) findViewById(R.id.textView1);
    tv.setText(sb1.toString());

    isPosted = true;
    } catch (UnsupportedEncodingException e) {
    Log.w(" error ", e.toString());
    } catch (Exception e) {
    Log.w(" error ", e.toString());
    }
    return false ;
    }

Servlet编码

     public class ClassDetails extends HttpServlet {

/**
 * 
 */
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request,
        HttpServletResponse response)
     throws ServletException, IOException
    {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String title = "Database Result";
    String docType =  "<!doctype html public \"-//w3c//dtd html 4.0 "  +"transitional//en\">\n";
    out.println(docType + "<html>\n" + "<head><title>" + title + "</title> </head>\n" + "<body bgcolor=\"#f0f0f0\">\n" +  "<h1 align=\"center\">" + title + " </h1>\n");

   String name = request.getParameter("name");
   String classs = request.getParameter("classs");
   String age = request.getParameter("age");
   if(sldept !=null)
   {
       out.println(" First: " + name + "<br>");
       out.println(" Last: " + classs + "<br>");
       out.println(" Last: " + age + "<br>");

   }
   else
   {
       out.println("no value");

      }   
    }
      }

0 个答案:

没有答案