2012-05-19 52 views
4

即時通訊嘗試在Android應用程序中連接mysql。以下是我的代碼。一旦我運行代碼,我得到'解析數據時出錯org.json.JSONException:在'字符0處輸入結束'錯誤。我用this教程android連接mysql

代碼

public class Test extends Activity{ 
    /** Called when the activity is first created. */ 
    @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      //setContentView(R.layout.mainabout); 
      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(); 

        HttpGet httppost = new HttpGet("http://www.pherma.net84.net/admin/getAllPeopleBornAfter.php"); 
       // httppost.s//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); 
        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()); 
      } 

      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()); 
      } 
     } 
    } 

logcat的 enter image description here

php 

<?php 
mysql_connect(".com","a4055820_root",""); 
mysql_select_db("a4055820_pherma"); 

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

print(json_encode($output)); 

mysql_close(); 
?> 
+0

通過以下鏈接進入: 1. HTTP: //堆棧溢出。 com/questions/6996000/error-in-http-connection 2. http://stackoverflow.com/questions/8202048/org-json-json-exception-end-of-input-at-character-0 –

回答

2

問題是我忘記了在清單中提供Internet訪問權限。

1

代碼似乎在第一個try catch塊意味着其餘的工作不會失敗。

您確定您創建的php文件的正確URL?

你的php文件是什麼樣的?

+0

please檢查答案... –

+0

從你給的所有我可以假設的是,連接沒有工作。你確定php文件位於這裏:「http://www.pherma.net84.net/admin/getAllPeopleBornAfter.php」。還有數據庫中的值? –

+0

其實只是注意到了這一行代碼,爲什麼它將它評論出來,這個註釋可能會失敗。 // httppost.s // setEntity(new UrlEncodedFormEntity(nameValuePairs)); –