0

ここに私のJavaコードがあります:

 @SuppressLint({ "ParserError", "ParserError" })
  public class MainActivity extends Activity {

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      InputStream is=null;
String result = "";
TextView tv=(TextView)findViewById(R.id.tv);
//the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name","shrey"));

//http post
 try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://10.0.2.2:8081/onlinechat");
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
       is = entity.getContent();

 }
 catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
//convert response to string
try{
    BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
        }
        is.close();

        result=sb.toString();
        tv.setText(result);

}catch(Exception e){
        Log.e("log_tag", "Error converting result "+e.toString());
}

//parse json data
   /*   try{
          JSONArray jArray = new JSONArray(result);
        for(int i=0;i<jArray.length();i++){
                JSONObject json_data = jArray.getJSONObject(i);
                Log.i("log_tag" , "name: "+json_data.getString("user")


                );
        }

}
 catch (JSONException e)
 {
        Log.e("log_tag", "Error parsing data "+e.toString());
   }
*/

 }


 }

ここに私のServletコードがあります:

  public class android extends HttpServlet {

/**
 * Processes requests for both HTTP
 * <code>GET</code> and
 * <code>POST</code> methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
   String user=request.getParameter("name").toString();
 /* BufferedReader reader = new BufferedReader( new  InputStreamReader(request.getInputStream() ) );  
                  String res = reader.readLine();  
     while( null != res )  
                   {  
                                         System.out.println( res );  
                            res = reader.readLine();  
                    }  

                  /*InputStream is = new        BufferedInputStream(request.getInputStream());      
        byte[] buf = new byte[8096]; 
            int bytesRead; 
            System.out.println("readbuf "+ is.read( buf )); 
                                while( ( bytesRead = is.read( buf ) ) != -1 )  
                os.write( buf, 0, bytesRead );  */  



    out.print("welcome"+user);
    out.println("success");




 }

  // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
 * Handles the HTTP
 * <code>GET</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
  @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
      processRequest(request, response);
     }

/**
 * Handles the HTTP
 * <code>POST</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
   @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
     processRequest(request, response);
   }

/**
 * Returns a short description of the servlet.
 *
 * @return a String containing servlet description
 */
   @Override
  public String getServletInfo() {
      return "Short description";
   }// </editor-fold>
    }

マニフェストにインターネット アクセス許可を追加しましたが、それでも実行されません。デバッガーが落ち着いた後のログキャットは次のとおりです

07-31 13:45:50.874: E/log_tag(666): Error converting result java.lang.NullPointerException
4

2 に答える 2

0

setcontentView を行っていません。だから私は tv.setText(result); で tv が null だと思います。Oncreate で setcontentView を実行する

于 2012-07-31T08:32:28.983 に答える
0

あなたの URL は、ワールド ワイドではなく、ローカル サブネットでのみ機能すると思います。一度確認してください。

于 2012-07-31T08:47:42.893 に答える