【问题标题】:Error trying to POST specified "id" to server for json response with Android Volley尝试使用 Android Volley 将指定的“id”发布到服务器以获取 json 响应时出错
【发布时间】:2016-03-13 07:48:52
【问题描述】:

我将产品 ID 从主要活动转移到详细活动,然后转移到 cmets 活动。我正在使用 Volley 和 Android Studio。我有一个从远程数据库填充的列表视图。当一个项目被点击时,项目的 id 使用一个意图传递给细节活动。在详细活动中,当用户单击 FAB 时,该产品 ID 也会使用意图传递给 cmets 活动。当我单击 FAB 时,cmets 活动打开但不显示任何内容。我是 Android Studio 中的 logcat,我收到以下错误:

评论:错误:org.json.JSONException: Value

这是用于检索指定产品 ID 的 cmets 的 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();

    }

}

产品在一个表中,而 cmets 在一个单独的表中。这就是为什么我有意传递“pid”的原因。我正在尝试仅获取与给定“pid”对应的 cmets 的 cmets 的 JSON 响应。 cmets 表如下所示:

  • 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 代码中一定有一些东西导致了错误

【问题讨论】:

    标签: php android json android-studio android-volley


    【解决方案1】:

    对于 JSON 解析,你应该有标头指定内容为 json。正如错误所示,您只是在 html 中回显 json 数据。当接收到的数据在 html 文件中时,您会得到这样的结果。尝试在 header 中将内容类型设置为 json

    在回显之前将header('Content-Type: application/json'); 放入您的php 文件中

    【讨论】:

    • 我很抱歉,但我不明白你的意思。我在编程方面没有那么先进。你能解释一下吗?
    • 这意味着您在响应正文中有 html 标签,例如 &lt;br&gt;,这在 json 中是不可接受的
    • 那我该如何解决呢?或者我怎样才能找到那个不需要的标签?
    • 我添加了您发布的标题,但仍然收到相同的错误
    • 能在浏览器中显示页面的来源吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    • 1970-01-01
    • 1970-01-01
    • 2013-06-15
    • 1970-01-01
    相关资源
    最近更新 更多