从android中的两个不同活动访问单个PHP文件

时间:2012-10-26 12:54:42

标签: java php android

我有两个活动:Mainpage.javaSecondary.java。我试图从这两个类访问相同的PHP文件。调用第二个类时,将撤消以下 错误

  java.lang.nullpointerexception.

如何删除此错误?

这导致应用程序强制关闭。 谢谢。 这是第一堂课的代码:     void login(){        试试{

       httpclient=new DefaultHttpClient();

       httppost= new HttpPost("http://10.0.2.2/ABC/login.php"); 
       nameValuePairs = new ArrayList<NameValuePair>(2);
       nameValuePairs.add(new BasicNameValuePair("username",et.getText().toString().trim()));  
       nameValuePairs.add(new BasicNameValuePair("password",pass.getText().toString().trim()));
       nameValuePairs.add(new BasicNameValuePair("action","LOGIN"));

       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

       ResponseHandler<String> responseHandler = new BasicResponseHandler();
       response=httpclient.execute(httppost);
       final String response = httpclient.execute(httppost, responseHandler);
       System.out.println("Response : " + response);


               tv.setText("Response from PHP : " + response);

               if(response.equalsIgnoreCase("User Found"))
               {
                   Toast.makeText(Axdroid.this,"Login Success",Toast.LENGTH_SHORT).show();   

                   startActivity(new Intent(getApplicationContext(),Secondary.class));
               }                   else
               {
                    showAlert();     
               }                                     

       dialog.dismiss();   


   }catch(Exception e){

       System.out.println("Exception : " + e.getMessage());
   }

}

这适用于中学课程     void jsr(){

    try{
        httpclient = new DefaultHttpClient();
        httppost= new HttpPost("http://10.0.2.2/AndroidLeave/login.php");
        nameValuePairs.add(new BasicNameValuePair("action","SUMMARY"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));


        response = httpclient.execute(httppost);

        System.out.println(" ++++++++  +++++++++++++"+response);                    

        entity = response.getEntity();
        is = entity.getContent();
    }catch(Exception e){
        Log.e("log_tag", "Error in http connection"+e.toString());
    }

    System.out.println(" ++++++++ After getting data from PHP file +++++++++++++");

    //convert response to string
    try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        sb = new StringBuilder();
        sb.append(reader.readLine() + "\n");
        String line="0";

        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }

        is.close();
        result=sb.toString();

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

logcat的:

 10-26 18:10:01.313: W/SingleClientConnManager(2296): Make sure to release the connection before allocating another one.
 10-26 18:10:01.964: E/log_tag(2296): Error in http connectionjava.lang.NullPointerException
 10-26 18:10:01.974: E/log_tag(2296): Error converting result java.lang.NullPointerException

2 个答案:

答案 0 :(得分:0)

发生错误是因为您尝试启动第二个连接,但未关闭第一个连接。 由于您没有在第一堂课中使用InputStream,因此您没有像第二节一样调用is.close()

我不太明白为什么在第一堂课中,你打两次电话execute()

你可以这样做: 删除这两行

ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);

并在此行之后

response=httpclient.execute(httppost);

添加:

final String responseString = EntityUtils.toString(response.getEntity());

然后使用responseStringresponse作为String。 如果仍然发生,请在此之后添加     EntityUtils.consume(response.getEntity()); toString也会消耗,但万一它不会消耗。

如果它仍在工作,请告诉我。

答案 1 :(得分:0)

只需快速查看代码,第二类中的这一行可能会发生异常:

nameValuePairs.add(new BasicNameValuePair("action","SUMMARY"));

因为nameValuePairs为空。您可以尝试添加以下内容:

nameValuePairs = new ArrayList<NameValuePair>(2);

在您尝试拨打nameValuePairs.add()

之前