2012-04-07 185 views
-1

您好,我試圖從json中的PHP腳本獲取數據。我得到幾個錯誤:第一個變量沒有解決。如果我嘗試添加像下面這樣的新變量,那麼在運行應用程序之後,我會收到一個錯誤,指出轉換錯誤。它主要是一個教程代碼,但正如我之前所說的那樣是一個IS變量的問題。你可以幫我嗎?連接到mysql的問題

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
    nameValuePairs.add(new BasicNameValuePair("year","1980")); 
    InputSteam is = null; 

    //http post 
    try{ 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://ik.su.lt/~jbarzelis/bandymas/getAllPeopleBornAfter.php"); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      InputStream 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","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()); 
    } 
} 
+0

首先你要問一個問題。 – ahanin 2012-04-07 14:53:26

+0

問題將是如何解決它或我缺少的。但我認爲這已經可以理解了。 – LTnewbie 2012-04-07 14:55:00

+0

請發佈實際的錯誤消息,或花更多的時間來解釋您的實際問題。 – Mat 2012-04-07 14:58:49

回答

1

我懷疑在InputStream聲明中簡單的拼寫錯誤可能是一個起點。

+0

真的很有幫助。我簡單插入InputStream is = null; (如上面的代碼)。但是這次我刪除了InputStream,它緊挨着is = entity.getContent();謝謝。 – LTnewbie 2012-04-07 15:06:32