连接到mysql的问题

时间:2012-04-07 14:48:39

标签: java android mysql

我正在尝试从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());
  }
}

1 个答案:

答案 0 :(得分:1)

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