2013-12-14 70 views
0

我有一個函數在我的Android應用程序發送數據到一個PHP文件插入到數據庫中。我的問題是post值永遠不會被髮送到PHP文件。Android應用程序不會發送數據到php文件

我的功能看起來是這樣的:

public void postData(){ 

    HttpClient client = new DefaultHttpClient(); 
    HttpPost post = new HttpPost(myURL); 
    try { 
     List<NameValuePair> param = new ArrayList<NameValuePair>(); 
     param.add(new BasicNameValuePair("firstName", "John")); 
     param.add(new BasicNameValuePair("lastName", "Doe")); 

     post.setEntity(new UrlEncodedFormEntity(param)); 
     HttpResponse response = client.execute(post); 

     BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 

      // show response in logcat 
      String line = ""; 
      while ((line = rd.readLine()) != null) { 
      Log.i("postData", line); 
      } 

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

我的PHP文件只是如果變量被設置檢查:

<?php 

if (isset($_POST["firstName"]) && isset($_POST["lastName"])) { 

    // insert into the database 

} else { 

    // send error message. 

} 
?> 

我永遠只是得到的其他部分的錯誤信息PHP文件。有人知道爲什麼會發生這種情況,或者如何解決這個問題?

+0

您在$ _REQUEST中收到了什麼 - 您可以登錄嗎? – Stan

回答

0

您的問題可能存在於跨域策略文件中。服務器必須有一個crossdomain.xml文件,允許服務器外部的用戶向其發送數據。

Google crossdomain.xml獲取更多信息。

相關問題