2016-03-13 31 views
0

我從主要活動攜帶產品ID到詳細活動,然後進行評論活動。我正在使用Volley和Android Studio。我有一個從遠程數據庫填充的列表視圖。當一個項目被點擊時,這些項目的id將通過一個意圖傳遞給該細節活動。在詳細的活動中,當用戶點擊FAB時,該產品ID也會使用意圖傳遞給評論活動。當我點擊FAB時,評論活動打開但沒有顯示任何內容。我的Android Studio中的logcat,我收到以下錯誤:錯誤嘗試POST指定的「ID」到服務器的JSON響應與Android Volley

Comments: Error: org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONArray

這是檢索指定產品ID註釋的PHP代碼:

<?php 
include_once("config.php"); 

$query=mysqli_query($con,"SELECT * FROM comments WHERE pid=".$_GET['pid']); 

$array; 
while($result=mysqli_fetch_assoc($query)){ 

$array[]=$result; 
} 

echo json_encode($array); 
?> 

詳細活動

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.commentsfab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       //Pass the image title and url to DetailsActivity 
       Intent intentTwo = new Intent(ProductsDetail.this, Comments.class); 
       intentTwo.putExtra("pid", pid); 
       intentTwo.putExtra("name", name); 
       intentTwo.putExtra("image", img); 
       intentTwo.putExtra("rating", rating); 

       //Start comments activity 
       startActivity(intentTwo); 
      } 
     }); 

評論動態

public class Comments extends AppCompatActivity { 
    private ProgressDialog dialog=null ; 
    private NetworkImageView cmntImg; 
    private TextView cmtjjTxt; 
    private RatingBar cmntRB; 
    private String TAG="Comments"; 
    private String tag_json_arry = "json_array_req"; 
    private String url = "http://192.168.0.5:80/demoapp"; 
    private String url_file="/comments.php"; 
    private CommentsAdapter adapter; 
    private ListView list; 
    ArrayList<CommentsRowData> cmntrowdata; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.comments); 

     final String pid = getIntent().getStringExtra("pid"); 
     final String name = getIntent().getStringExtra("name"); 
     final String img = getIntent().getStringExtra("image"); 
     final String rating = getIntent().getStringExtra("rating"); 

     list=(ListView)findViewById(R.id.commentsList); 
     cmntrowdata=new ArrayList<CommentsRowData>(); 

     dialog= new ProgressDialog(this); 

     dialog.setMessage("Loading..."); 
     dialog.show(); 

     cmntImg = (NetworkImageView)findViewById(R.id.picComments); 
     cmntImg.setImageUrl(img, VolleyController.getInstance().getImageLoader()); 
     cmtjjTxt = (TextView)findViewById(R.id.titleComments); 
     cmtjjTxt.setText(name); 
     cmntRB = (RatingBar)findViewById(R.id.ratingBar3); 
     cmntRB.setRating(Float.valueOf(rating)); 

     JsonArrayRequest request = new JsonArrayRequest(url+url_file, 
       new Response.Listener<JSONArray>() { 
        @Override 
        public void onResponse(JSONArray response) { 
         Log.d(TAG, response.toString()); try { 
          for(int i=0;i<response.length();i++){ 
           String pid=response.getJSONObject(i).getString("pid"); 
           String cid=response.getJSONObject(i).getString("cid"); 
           String username = response.getJSONObject(i).getString("username"); 
           String comment = response.getJSONObject(i).getString("comment"); 

           cmntrowdata.add(new CommentsRowData(pid,cid,username,comment)); 
          } 
         } catch (JSONException e) { 

          e.printStackTrace(); 
         } 
         adapter=new CommentsAdapter(Comments.this, cmntrowdata); 
         list.setAdapter(adapter); 
         dialog.dismiss(); 
        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       Log.d(TAG, "Error: " + error.getMessage()); 
       dialog.dismiss(); 
      } 

      //@Override 
      protected Map<String, String> getParams(){ 
       Map<String,String> params = new HashMap<String,String>(); 
       params.put("pid",pid); 
       return params; 
      } 
     }); 

     VolleyController.getInstance().addToRequestQueue(request, tag_json_arry); 




    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 

    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 

    } 

} 

產品在一張表中,並且註釋位於單獨的表中。這就是爲什麼我要通過意圖傳遞「pid」。我正在嘗試僅針對與給定「pid」相對應的註釋獲取註釋的JSON響應。 comments表看起來是這樣的:

  • PID(產品ID)
  • CID(註釋ID)
  • 用戶名
  • 評論

編輯 如果我輸入以下網址在我的瀏覽器中,

http://localhost/demoapp/comments.php?sid=2

我得到以下JSON響應..

[ 
    { 
     "pid": "2", 
     "cid": "1", 
     "username": "john", 
     "comment": "one of my favorites" 
    }, 
    { 
     "pid": "2", 
     "cid": "2", 
     "username": "jane", 
     "comment": "this was so yummy" 
    } 
] 

因此,必須有我的Android代碼中的東西,導致錯誤

回答

1

對於JSON解析ü應該有頭指定的內容是JSON。由於錯誤提示你只是在html中回顯json數據。當收到的數據在html文件中時,你會得到這樣的結果。嘗試呼應

+0

我很抱歉,之前設置的內容類型的JSON在頭

header('Content-Type: application/json');在你的PHP文件,但我不明白你的意思。我不是那種先進的編程。你能再解釋一下嗎? –

+0

這意味着你在響應人身上有html標籤,比如'
',這在json中是不可接受的 –

+0

那麼我該如何解決這個問題呢?或者,如何找到不需要的標籤? –

相關問題