错误尝试后指定的" ID"到服务器与Android排球JSON响应 [英] Error trying to POST specified "id" to server for json response with Android Volley

查看:440
本文介绍了错误尝试后指定的" ID"到服务器与Android排球JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我背着在产品ID从主活动细节活动再上一个意见的活动。我使用的排球和Android的工作室。我已经从远程数据库填充列表视图。当点击一个项目,项目ID传递给使用意图的详细活动。在具体活动中,当用户点击一个FAB,产品ID传递到的意见也活动使用意图。当我点击了FAB的评论活动打开,但不显示任何内容。我的Andr​​oid Studio中logcat中,我得到以下错误:

I am carrying over a product id from the main activity to a detail activity then on to a comments activity. I am using Volley and Android Studio. I have a listview populated from a remote database. When an item is clicked, the items' id is passed to the detail activity using an intent. In the detail activity, when the user clicks on a FAB, that products id is passed over to the comments activity also using an intent. When I click on the FAB the comments activity opens but doesn't show anything. I the logcat within Android Studio, I get the following error:

编辑点评:错误:org.json.JSONException:值< BR型
  java.lang.String中不能转换为JSONArray

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

这是PHP code检索的评论指定产品ID:

This is the PHP code for retrieving the comments for the specified product id:

    <?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表看起来是这样的:

Products are in one table and the comments are in a separate table. Thats why I'm passing the "pid" over with an intent. I'm trying to get the JSON response of comments for only comments that correspond with the given "pid". The comments table looks like this:


  • PID(产品ID)

  • CID(注释ID)

  • 用户名

  • 注释

修改
如果我在浏览器中输入下面的网址,

EDIT If I enter the following URL in my browser,

HTTP://localhost/demoapp/comments.php SID = 2

我得到以下JSON响应。

I get the following JSON response..

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

因此​​,必须有我的Andr​​oid code之内的东西,导致错误

So there has to be something within my Android code that is causing the error

推荐答案

有关JSON解析ü应该有头指定的内容是JSON。由于错误提示你只是呼应了HTML的JSON数据。你得到这样当接收到的数据为HTML文件。尝试设置内容类型为JSON在头

For JSON parsing u should have header specifying the content to be json. As the error suggests you are just echoing the json data in the html. You get such when the received data is in html file. Try setting the content type to json in the header

标题(内容类型:应用程序/ JSON'); 在你的PHP文件之前呼应

put header('Content-Type: application/json'); in your php file before echoing

这篇关于错误尝试后指定的&QUOT; ID&QUOT;到服务器与Android排球JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆