2013-09-01 34 views
-1

這裏我的代碼從MySQL服務器提取數據:Android的 - 使用PHP

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 

     StrictMode.setThreadPolicy(policy); 


     String result = ""; 
     //the year data to send 
     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
     nameValuePairs.add(new BasicNameValuePair("year","1980")); 
     InputStream is = null; 
     //http post 
     try{ 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("dropbox link to php code"); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      is = entity.getContent(); 
      System.out.println(is); 
     }catch(Exception e){ 
      Log.e("log_tag", "Error in http connection "+e.toString()); 
     } 
     //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(); 
      System.out.println("1 " + 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","id: "+json_data.getInt("id")+ 
         ", name: "+json_data.getString("name")+ 
         ", sex: "+json_data.getInt("sex")+ 
         ", birthyear: "+json_data.getInt("birthyear") 
         ); 
      } 
     } 
     catch (JSONException e){ 
      Log.e("log_tag", "Error parsing data "+e.toString()); 
     } 
    } 
} 

在HTTPPost鏈接是一個下載鏈接到Dropbox的PHP代碼。該代碼如下所示:

<?php 
mysql_connect("host","username","password"); 
mysql_select_db("1487057_test"); 

$q=mysql_query("SELECT * FROM people WHERE birthyear>'".$_REQUEST['year']."'"); 
while($e=mysql_fetch_assoc($q)) 
     $output[]=$e; 

print(json_encode($output)); 

mysql_close(); 
?> 

問題是PHP代碼似乎並未被HTTPClient「執行」。當我做輸入流的打印時,我只返回PHP代碼的前兩行。 JSON日誌似乎根本不打印。任何人看到有什麼問題?

+0

你可以發佈出嗎? – KOTIOS

+0

輸出什麼? –

+0

輸出的php? 2行php代碼? – KOTIOS

回答

1

如果你從FTP服務器JSON數據將使用dropbox來PHP文件做工精細,因爲烏爾 ,U?需要確保它的工作原理, 建議你先在瀏覽器上測試服務,如果它工作正常,那麼你可以使用code.possibly設置ftp而不是Dropbox爲 文件服務器。

0

修改你的PHP是這樣的:

<?php 
$sql = "SELECT * FROM people WHERE birthyear>'".$_REQUEST['year']."'"; 

mysql_query("set names utf8"); 
$result = mysql_query($sql); 
while ($e = mysql_fetch_assoc($result)) { 
    $output[]=$e; 
} 

print(json_encode($output)); 
mysql_close($dbhost); 

>