4

私はAndroidアプリでmysqlに接続しようとしています。以下は私のコードです。コードを実行すると、'データの解析中にエラーが発生しますorg.json.JSONException: 'エラーの文字0で入力が終了します。私はこのチュートリアルを使用していました

コード

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 ここに画像の説明を入力してください

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

3 に答える 3

2

問題は、マニフェストでインターネットアクセス許可を与えるのを忘れたことでした。

于 2012-05-23T08:46:44.260 に答える
1

コードは最初のtrycatchブロックで失敗するようです。つまり、残りは機能しません。

作成したphpファイルへの正しいURLを確認しますか?

あなたのphpファイルはどのように見えますか?

于 2012-05-19T17:43:05.100 に答える
1

信頼できるおよび/または公式の情報源

次のリンクを通過します。

  1. http接続のエラー
  2. org.json.JSON例外:文字0での入力の終了
  3. http://androidforums.com/application-development/217771-parsing-data-using-json-php-but-receiveing-error-parsing-data-org-json-exception.html
于 2012-05-23T08:56:00.183 に答える