-1

您好正在尝试从 json 中的 php 脚本获取数据。我得到一些错误:首先是变量没有解决。如果我尝试像下面这样添加新变量,那么在运行应用程序后我会收到错误消息,显示转换错误。它主要是一个教程代码,但正如我之前所说,IS 变量存在问题。你能帮助我吗?

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

1 回答 1

1

我怀疑声明中的简单错字InputStream可能是一个开始的地方。

于 2012-04-07T15:00:40.993 回答