2011-09-23 55 views
1

我對Android和php很新穎。我正在關注一個網站上的一個竅門,並製作了一個程序,發送和收集我的數據庫中出生的人的信息。不過,我得到這些logcat的錯誤:如何解決Android程序和MySQL之間的連接問題

09-23 18:30:04.146: ERROR/log_tag(16030): Error in http connection java.net.UnknownHostException: www.enjoyen.in 
09-23 18:30:04.146: ERROR/log_tag(16030): Error converting result java.lang.NullPointerException 
09-23 18:30:04.154: ERROR/log_tag(16030): Error parsing data org.json.JSONException: End of input at character 0 of 

我不知道我應該怎麼解決這個問題

這是我的java文件:

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


@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 

    String result = null; 
    InputStream is = null; 
    StringBuilder sb=null; 

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

    //http post 
    try{ 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://mywebsite/sampleDB/HAconnect.php"); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      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); 
      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","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()); 
    } 

} 
} 

這是我的PHP文件:

<html> 
<body> 
<?php 
mysql_connect("localhost","user","password"); 
mysql_select_db("database"); 

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

print(json_encode($output)); 

mysql_close(); 
?> 
</body> 
</html> 

我該怎麼辦?

回答

2

嘗試通過「android UnknownHostException」進行搜索。這是模擬器問題。要解決它嘗試重新啓動模擬器或重新創建avd

+0

我實際上使用我的手機(摩托羅拉Droid)。你如何創建avd? – Eric

+0

Avd - android虛擬設備。爲了創建它,啓動SDKManager - 虛擬設備 - 新建 - 選擇選項和API級別 - 創建。 –

+0

哈哈,我發現我的錯誤。我沒有INTERNET的許可。謝謝你。 – Eric