2011-04-11 266 views
0

我一直在嘗試這個教程。它沒有顯示任何錯誤,但在模擬器上只顯示我給出的URL!將android連接到mysql數據庫

我的連接字符串是:

public static final String KEY_121 = "http://10.0.2.2/index.php"; 

(我用10.0.2.2作爲我工作的本地主機上)

當我檢查了我的logcat這表明我DIS:

error parsing data org.json.JsonException: Value <br of type java.lang.String cannot be converted to JSONArray 

我真的不知道我的代碼有什麼問題。誰能幫幫我嗎。我最後一年的項目真的需要幫助。

這是我的Android的Java代碼

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class main extends Activity { 
    /** Called when the activity is first created. */ 

    TextView txt1, txt2; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     // Create a crude view - this should really be set via the layout 
     // resources 
     // but since its an example saves declaring them in the XML. 
     txt1 = (TextView) findViewById(R.id.ip); 

     LinearLayout rootLayout = new LinearLayout(getApplicationContext()); 
     txt1 = new TextView(getApplicationContext()); 
     rootLayout.addView(txt1); 
     setContentView(rootLayout); 

     // Set the text and call the connect function. 
     txt1.setText("Connecting..."); 
     // call the method to run the data retreival 
     txt1.setText(getServerData(KEY_121)); 

    } 

    public static final String KEY_121 = "http://10.0.2.2/index.php"; 

    private String getServerData(String returnString) { 

     InputStream is = null; 

     String result = ""; 
     // the year data to send 
     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
     nameValuePairs.add(new BasicNameValuePair("year", "1970")); 


     try { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(KEY_121); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      is = entity.getContent(); 

     } 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(); 
     } 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", 
         "longitude: " + json_data.getDouble("longitude") 
           + ", latitude: " 
           + json_data.getDouble("latitude")); 
       // Get an output to the screen 
       returnString += "\n\t" + jArray.getJSONObject(i); 
      } 
     } catch (JSONException e) { 
      Log.e("log_tag", "Error parsing data " + e.toString()); 
     } 

     return returnString; 
    } 

} 

這是我的PHP代碼

<?php 

mysql_connect("localhost","root","sugi"); 
mysql_select_db("android"); 

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

while($e=mysql_fetch_assoc($q)) 
     $output[]=$e; 

print(json_encode($output)); 

mysql_close(); 
?> 

這是MySQL查詢

CREATE TABLE people (

    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY , 

    name VARCHAR(100) NOT NULL , 

    sex BOOL NOT NULL DEFAULT '1', 

    birthyear INT NOT NULL 

) 

這是當我輸入什麼它顯示「http://10.0.2.2/index.php 「on IE

enter image description here

回答

0

嘗試打印從服務器獲得的輸出。它似乎不清楚返回的JSON。這部分

Value <br of type 

看起來好像有一個難以進入的地方。

0

修復你的PHP警告和index.php的結果應該是一個有效的JSON響應。

變量$_REQUEST['year']未定義,可能是因爲您沒有將它與您的請求一起發送。您的變量nameValuePairs永遠不會添加到您的請求......

+0

嗨,非常感謝幫助。我是PHP和MySQL的新手。你能告訴我,我需要做什麼來發送我的請求? – sugianto 2011-04-12 01:56:17

0

服務器可能返回與

<BR /> 

開始,您可以通過添加一行代碼塊驗證這樣的錯誤:

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"); 

      ///////////////////////////////// 
      // ADD THIS 
      Log.i("log_tag","Line reads: " + line); 

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

然後運行該程序並檢查您的日誌。它會顯示你從服務器收到的信息。例如,如果你的PHP配置不包含JSON庫,你將會得到一個錯誤。您可能正在使用舊版本的PHP。如果您使用託管服務器,如1and1或GoDaddy,則可能只需編輯.htaccess文件,以便您的站點使用PHP5。