2012-01-03 30 views
0

我試圖解析數據到PHP文件和使用jsons.Reit數據它給出錯誤。 「解析數據時出錯org.json.JSONException:輸入字符0處的輸入結束」 我在做什麼錯了?非常感謝您的幫助。問題 - 解析數據到php和jsons

 public static final String KEY_121 ="http://10.0.2.2/reports.php"; //i use my real ip here 


    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 ("typ",SpinnerValue)); 
     nameValuePairs.add(new BasicNameValuePair ("Nam",AutoCompleteValue)); 
     nameValuePairs.add(new BasicNameValuePair ("frm","2011-09-28")); 
     nameValuePairs.add(new BasicNameValuePair ("to","2011-10-09")); 


     //http post 

     try 
     { 

     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(KEY_121); 
     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); 
      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()); 
     } 


     //parse json data 

      try 
       { 
       JSONArray jArray = new JSONArray(result); 
       for(int i=0;i<jArray.length();i++) 
       { 
         JSONObject json_data = jArray.getJSONObject(i); 

         // String id = json_data.getString("id"); 
         name =json_data.getString("Name"); 
         Path= json_data.getString("Path"); 
         Toast.makeText (getApplicationContext(), Path,      
          Toast.LENGTH_SHORT).show(); 

         //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 FILE** 


      <?php 

       mysql_connect("localhost:3306","root",""); 

       mysql_select_db("infosoft"); 

       $q=mysql_query"SELECT * FROM report WHERE FrmDte>='".$_REQUEST["frm"]."' AND 

       ToDte<='".$_REQUEST["to"]."' AND Name='".$_REQUEST["Nam"]."' AND 
       Type='".$_REQUEST["typ"]."' "); 



       while($e=mysql_fetch_assoc($q)) 

       $output[]=$e; 

       print(json_encode($output)); 

       mysql_close(); 
       ?> 

回答

2

在你的PHP代碼mysql_query("Your query here")是一種方法。你忘了括號。

$output = array(); 
while($e = mysql_fetch_assoc($q)) { 
    $output[] = $e; 
} 
echo json_encode($output);