2011-04-11 13 views
0

私はこのチュートリアルを試しています。それはすべてのエラーを表示するが、エミュレータでは、私が与えたURLだけが表示されます!android to mysql database

私の接続文字列だった:私は私のlogcatをチェックすると

public static final String KEY_121 = "http://10.0.2.2/index.php"; 

(私はローカルホスト上で働いていると私は10.0.2.2を使用)

それは私にDISを示した:

error parsing data org.json.JsonException: Value <br of type java.lang.String cannot be converted to JSONArray 

私のコードで何が間違っているのか分かりません。誰でも助けてくれますか?私は最後の1年間のプロジェクトのためにこれについて本当に助けが必要です。

これは、これは私のPHPコード

<?php 

mysql_connect("localhost","root","sugi"); 
mysql_select_db("android"); 

$q=mysql_query("SELECT * FROM people WHERE birthyear>'".$_REQUEST['year']."'"); 

while($e=mysql_fetch_assoc($q)) 
     $output[]=$e; 

print(json_encode($output)); 

mysql_close(); 
?> 

である私のAndroidのJavaコード

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class main extends Activity { 
    /** Called when the activity is first created. */ 

    TextView txt1, txt2; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     // Create a crude view - this should really be set via the layout 
     // resources 
     // but since its an example saves declaring them in the XML. 
     txt1 = (TextView) findViewById(R.id.ip); 

     LinearLayout rootLayout = new LinearLayout(getApplicationContext()); 
     txt1 = new TextView(getApplicationContext()); 
     rootLayout.addView(txt1); 
     setContentView(rootLayout); 

     // Set the text and call the connect function. 
     txt1.setText("Connecting..."); 
     // call the method to run the data retreival 
     txt1.setText(getServerData(KEY_121)); 

    } 

    public static final String KEY_121 = "http://10.0.2.2/index.php"; 

    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("year", "1970")); 


     try { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(KEY_121); 
      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()); 
     } 
     // 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", 
         "longitude: " + json_data.getDouble("longitude") 
           + ", latitude: " 
           + json_data.getDouble("latitude")); 
       // 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; 
    } 

} 

ですこれは、MySQLのクエリ

CREATE TABLE people (

    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY , 

    name VARCHAR(100) NOT NULL , 

    sex BOOL NOT NULL DEFAULT '1', 

    birthyear INT NOT NULL 

) 

これは、その表示私はhttp://10.0.2.2/index.php「入力したときに何であります"on IE

enter image description here

答えて

0

サーバーからの出力を試してみてください。 JSONが戻ってくるのは明らかではないようです。この部分

Value <br of type 

どこか難しいところがあるようです。

0

PHPの警告を修正し、index.phpの結果が有効なJSON応答である必要があります。

変数$_REQUEST['year']が定義されていません。これはおそらく、あなたのリクエストとともに送信しなかったからです。あなたの変数nameValuePairsがあなたの要求に追加されることはありません...

+0

こんにちは、ありがとうございました。私はphpとmysqlの初心者です。私の要求を送るために何が必要なのか教えていただけますか? – sugianto

0

サーバーは、おそらくあなたは、コードのこのブロックに行を追加することでこれを確認することができます

<BR /> 

で始まるエラーを返して:

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

次に、プログラムを実行してログを確認します。サーバーから受け取った情報を表示します。

たとえば、PHP設定にJSONライブラリが含まれていないと、エラーが発生します。古いバージョンのPHPを使用している可能性があります。 1and1やGoDaddyなどの管理対象サーバを使用している場合は、サイトでPHP5を使用するように.htaccessファイルを編集するだけで済みます。