2016-03-07 87 views
1

我知道如何使用Volley GET方法登录的目的,但如果我需要指定一个特定的“ID”?我使用intent.putExtra将远程数据库中列表项的“id”传递给另一个活动。在下一个活动中,我将如何利用Volleys的GET方法使用通过“id”传递的另一个表来解析和显示数据?如何使用指定参数的Android Volley GET方法?

在浏览器中,我可以使用以下方法来获取与指定的URL和参数的响应:

http://localhost/demoapp/fetch.php?pid=2 

但我不知道如何该网址传递给凌空请求,并显示在响应一个listview。例如:

"http://localhost/demoapp/fetch.php?pid=" + pid 

其中“pid”将是相应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); 
?> 

编辑2:Java代码 主要活动

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 name=response.getJSONObject(i).getString("product_name"); 
           String img; 
           String thumb = response.getJSONObject(i).getString("product_thumb"); 
           String detail = response.getJSONObject(i).getString("product_detail"); 
           String rating = response.getJSONObject(i).getString("product_rating"); 

           img = response.getJSONObject(i).getString("product_pic"); 

           rowdata.add(new ProductRowData(pid,name,img,thumb,detail,rating)); 
          } 
         } catch (JSONException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
         adapter=new ProductsAdapter(MainActivity.this, rowdata); 
         list.setAdapter(adapter); 
         dialog.dismiss(); 
        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       Log.d(TAG, "Error: " + error.getMessage()); 
       dialog.dismiss(); 
      } 
     }); 

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

当单击列表项,数据的其余部分被传递到细节使用intent.putExtra的活动。然后我试图做的是,在点击FAB时,应该显示来自不同表格和该产品ID的评论列表。

+0

什么你用GET方法面临的问题是什么?你似乎正在遵循正确的程序。 –

+0

@JyotmanSingh试图找出如何在Android应用程序中检索该json响应。请参阅我的下一个编辑。 –

+1

你提到你知道如何使用Volley GET方法。以类似于您用于登录的方式使用它。使用'JsonObjectRequest'来获取json数据,然后解析它并在你的应用程序中显示它。 –

回答

2

如果你想使用GET,你可以创建一个URL的请求参数:

String uri = String.format("http://localhost/demoapp/fetch.php?pid=%1$s", pid); 

StringRequest myReq = new StringRequest(Method.GET, 
            uri, 
            createMyReqSuccessListener(), 
            createMyReqErrorListener()); 

或者更好的,你可以使用POST:

StringRequest stringRequest = new StringRequest(Request.Method.POST, "http://localhost/demoapp/fetch.php", new Response.Listener<String>() 
{ 
    @Override 
    public void onResponse(String s) 
    { 
    } 
}, new Response.ErrorListener() 
{ 
    @Override 
    public void onErrorResponse(VolleyError volleyError) 
    { 
    } 
}) 
{ 
    @Override 
    protected Map<String, String> getParams() 
    { 
     Map<String, String> params = new HashMap<String, String>(); 
     params.put("id", pid); 
     return params; 
    } 
}; 

来自Volley - POST/GET parameters

+0

如果我使用POST方法,是否必须将PHP代码更改为'。$ _ POST ['pid']'而不是'。$ _ GET ['pid']'? –

+0

是的,你必须改变你的后端。 – dan

+0

如果我在我的PHP中更改方法,并尝试使用具有添加的参数的相同url,则我得到一个未定义的索引ID –

相关问题