2011-12-05 25 views
0

jsonencode($出力)を出力するsimpe PHPスクリプトを作成しました。先頭と末尾の[]でJSONを出力します。私はAndroidのプログラムをemulaotorで実行すると、JSONObjectのテキストは{.....の文字1で始まり、次にJSONのリストがあるので、そのデータを取得していることがわかりますそれらの角括弧を削除しないデコード処理ここでは、ログにエラーを投げている機能は次のとおりです。PHP、JSONエラーでMySQLに接続

public class JSONfunctions { 

public static JSONObject getJSONfromURL(String url){ 
    InputStream is = null; 
    String result = ""; 
    JSONObject jArray = null; 

    //http post 
    try{ 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(url); 
      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{ 

     jArray = new JSONObject(result);    
    }catch(JSONException e){ 
      Log.e("log_tag", "Error parsing data "+e.toString()); 
    } 

    return jArray; 
} 

}

答えて

0

あなたのJSONレスポンスがどのように見えるようですね:[{'name':'value'},...]

あなたのJSONオブジェクトは、エラーメッセージのような{で始まる必要があります言った。それは次のようになります。
{'my_array':[.......]}

次にあなたが書くことができる:new JSONObject().getJSONArray("my_array");

+0

しかし、デフォルトの印刷(jsonencode($出力)); PHPからは、タイトルと大括弧で囲まれていません。これを変更する方法はありません。 – savagenoob

関連する問題