<?php $con = mysql_connect(&quot 如何转成引号;localhost&quot 如何转成引号;,&quot 如何转成引号;①&quot 如何转成引号;,&quot 如何转成引号;①&quot 如何转成引号;);

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:
Comments: Error: org.json.JSONException: Value &br of type
java.lang.String cannot be converted to JSONArray
This is the PHP code for retrieving the comments for the specified product id:
include_once("config.php");
$query=mysqli_query($con,"SELECT * FROM comments WHERE pid=".$_GET['pid']);
while($result=mysqli_fetch_assoc($query)){
$array[]=$
echo json_encode($array);
Detail Activity
FloatingActionButton fab = (FloatingActionButton) findViewById(mentsfab);
fab.setOnClickListener(new View.OnClickListener() {
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);
Comments Activity
public class Comments extends AppCompatActivity {
private ProgressDialog dialog=
private NetworkImageView cmntI
private TextView cmtjjT
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 CommentsA
private ListV
ArrayList&CommentsRowData&
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(ments);
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(mentsList);
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&() {
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() {
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);
VolleyController.getInstance().addToRequestQueue(request, tag_json_arry);
protected void onResume() {
super.onResume();
protected void onPause() {
super.onPause();
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(product id)
cid(comment id)
If I enter the following URL in my browser,
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"
So there has to be something within my Android code that is causing the error
解决方案 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
put header('Content-Type: application/json'); in your php file before echoing
本文地址: &
我背着在产品ID从主活动细节活动再上一个意见的活动。我使用的排球和Android的工作室。我已经从远程数据库填充列表视图。当点击一个项目,项目“ID传递给使用意图的详细活动。在具体活动中,当用户点击一个FAB,产品ID传递到的意见也活动使用意图。当我点击了FAB的评论活动打开,但不显示任何内容。我的Android Studio中logcat中,我得到以下错误:
编辑点评:错误:org.json.JSONException:值< BR型
java.lang.String中不能转换为JSONArray 这是PHP code检索的评论指定产品ID: < PHPinclude_once(“config.php中”);$查询= mysqli_query($ CON,“SELECT * FROM评论其中pid =”$ _ GET ['PID']);$阵列;而($结果= mysqli_fetch_assoc($查询)){$数组[] = $结果;}回声json_en code($数组);?>
FloatingActionButton晶圆厂=(FloatingActionButton)findViewById(mentsfab);
fab.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(查看视图){
//把图像标题和URL来DetailsActivity
意图intentTwo =新意图(ProductsDetail.this,Comments.class);
intentTwo.putExtra(“PID”,PID);
intentTwo.putExtra(“名”,名);
intentTwo.putExtra(“形象”,IMG);
intentTwo.putExtra(“等级”,评级);
//开始评论活动
startActivity(intentTwo);
公共类评论扩展AppCompatActivity {
私人ProgressDialog对话框= NULL;
私人NetworkImageView cmntI
私人TextView的cmtjjT
私人的RatingBar cmntRB;
私人字符串标记=“评论”;
私人字符串tag_json_arry =“json_array_req”;
私人字符串URL =“http://192.168.0.5:80/demoapp”;
私人字符串url_file =“/ comments.php文件”;
私人CommentsAdapter适配器;
私人ListView控件列表;
ArrayList的< CommentsRowData>
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(ments);
最后弦乐PID = getIntent()getStringExtra(“PID”)。
最终的字符串名称= getIntent()getStringExtra(“名称”)。
最后弦乐IMG = getIntent()getStringExtra(“图像”)。
最后弦乐等级= getIntent()getStringExtra(“等级”)。
名单=(ListView控件)findViewById(mentsList);
cmntrowdata =新的ArrayList< CommentsRowData>();
对话框=新ProgressDialog(本);
dialog.setMessage(“正在加载...”);
dialog.show();
cmntImg =(NetworkImageView)findViewById(R.id.picComments);
cmntImg.setImageUrl(IMG,VolleyController.getInstance()getImageLoader());
cmtjjTxt =(的TextView)findViewById(R.id.titleComments);
cmtjjTxt.setText(名);
cmntRB =(的RatingBar)findViewById(R.id.ratingBar3);
cmntRB.setRating(Float.valueOf(等级));
JsonArrayRequest要求=新JsonArrayRequest(URL + url_file,
新Response.Listener< JSONArray>(){
公共无效onResponse(JSONArray响应){
Log.d(TAG,response.toString());尝试{
的for(int i = 0; I< response.length();我++){
字符串PID = response.getJSONObject(I).getString(“PID”);
串CID = response.getJSONObject(ⅰ).getString(“CID”);
字符串username = response.getJSONObject(I).getString(“用户名”);
字符串评论= response.getJSONObject(I).getString(“评论”);
cmntrowdata.add(新CommentsRowData(PID,CID,用户名,评论));
}赶上(JSONException E){
e.printStackTrace();
适配器=新CommentsAdapter(Comments.this,cmntrowdata);
list.setAdapter(适配器);
dialog.dismiss();
},新Response.ErrorListener(){
公共无效onErrorResponse(VolleyError错误){
Log.d(TAG,“错误:”+ error.getMessage());
dialog.dismiss();
保护地图<字符串,字符串> getParams()方法{
地图<字符串,字符串> PARAMS =新的HashMap<字符串,字符串>();
params.put(“PID”,PID);
返回PARAMS;
。VolleyController.getInstance()addToRequestQueue(请求,tag_json_arry);
保护无效onResume(){
super.onResume();
保护无效的onPause(){
super.onPause();
}} 产品都在一个表和意见是在单独的表。这就是为什么我传递“PID”在与意图。我试图让只为给定的“PID”对应评论评论JSON响应。 comments表看起来是这样的: PID(产品ID) CID(注释ID)用户名注释 修改如果我在浏览器中输入下面的网址,
我得到以下JSON响应。
“PID”:“2”,
“CID”:“1”,
“用户名”:“约翰”,
“意见”:“我的最爱之一”
“PID”:“2”,
“CID”:“2”,
“用户名”:“珍”,
“意见”:“这是如此美味”
}] 因此,必须有我的Android code之内的东西,导致错误解决方案 有关JSON解析ü应该有头指定的内容是JSON。由于错误提示你只是呼应了HTML的JSON数据。你得到这样当接收到的数据为HTML文件。尝试设置内容类型为JSON在头把标题(“内容类型:应用程序/ JSON'); 在你的PHP文件之前呼应
本文地址: &
扫一扫关注官方微信php连接远程mysql数据库实例-Php与数据库-Php教程-壹聚教程网php连接远程mysql数据库实例
在php中如果我们要连接远程数据库连接方法也很简单,我们只要把本地连接localhost或127.0.0.1改成指定远程服务器一IP地址即可。
mysql_connect(servername,username,pass);
在下面的例子中,我们在一个变量中 ($con) 存放了在脚本中供稍后使用的连接。如果连接失败,将执行 &die& 部分:
$con = mysql_connect(&localhost&,&peter&,&abc123&);
if (!$con)
& die('Could not connect: ' . mysql_error());
// some code
上面是连接本地,下面把localhost改成远程IP即可了
$conn=mysql_connect('150.125.221.25','root','123');
if(!$conn) echo &失败!&;
&&& else echo &成功!&;
&&&& // 从表中提取信息的sql语句
&&& $sql=&SELECT * FROM user where userName='$user_name'&;
&&& // 执行sql查询
&&& $result=mysql_db_query('info', $sql, $conn);
&&& // 获取查询结果
&&& $row=mysql_fetch_row($result);
&mysql_close();&&&&&&&&
上一页: &&&&&下一页:相关内容

我要回帖

更多关于 quot什么意思 的文章

 

随机推荐