2013-11-01 24 views
0

我是android新手。我想调用简单的PHP脚本,回显一些基本的字符串,并且我想在android应用程序中的textView中显示该字符串。但是当我点击我的按钮调用PHP脚本没有任何事情发生。这里是我的Android代码:android和php脚本

public void phpConnection(View v){ 
    et = (EditText)findViewById(R.id.edit_message); 
    final ProgressDialog p = new  ProgressDialog(v.getContext()).show(v.getContext(),"Waiting for Server", "Accessing Server"); 
    //TextView tv = (TextView)findViewById(R.id.tv); 
    //tv.setText("Response from PHP"); 
    Thread thread = new Thread() 
    { 
     @Override 
     public void run() { 
      try{ 

       httpclient=new DefaultHttpClient(); 
       httppost= new HttpPost("http://127.0.0.1/testic.php"); // make sure the url is correct. 
       //add your data 
       nameValuePairs = new ArrayList<NameValuePair>(1); 
       // Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar, 
       nameValuePairs.add(new BasicNameValuePair("Edittext_value",et.getText().toString().trim())); // $Edittext_value = $_POST['Edittext_value']; 
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       //Execute HTTP Post Request 
       response=httpclient.execute(httppost); 

       ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
       final String response = httpclient.execute(httppost, responseHandler); 
       System.out.println("Response : " + response); 
       runOnUiThread(new Runnable() { 
         public void run() { 
          p.dismiss(); 
          TextView tv = (TextView)findViewById(R.id.tv); 
          tv.setText("Response from PHP" + response); 
         } 
        }); 

      }catch(Exception e){ 

       runOnUiThread(new Runnable() { 
        public void run() { 
         p.dismiss(); 
        } 
       }); 
       System.out.println("Exception : " + e.getMessage()); 
      } 
     } 
    }; 

    thread.start(); 


} 

,并在这里很简单testic.php脚本:

<?php 

$val="My name is Nedim"; 
echo $val; 
} 
?> 

这个脚本保存在服务器WAMP。 请问谁能告诉我哪里是错误。

+1

我认为_you_需要告诉_us_错误在哪里,而不是相反。你的logcat会说什么吗?你做了什么来尝试调试这个(除了看到按钮不起作用并在这里发布)? –

+0

尝试在您的移动浏览器中运行http://127.0.0.1/testic.php。如果没有出现,原因很简单。 Android设备无法看到本地服务器及其页面。 –

回答

0

看看apache的日志 - > access.log | other_vhosts_access.log | error.log中,如果你看到的东西从你的应用程序来;) - 或者你关闭你的apache并运行“netcat的”,如:

nc -vvlp80 
这之后,您尝试运行你的应用程序

,你会看到HTTP在窗口中的请求