2014-02-11 2 views
1

누군가가이 오류를 수정하기 위해 어떤 도움을 줄 수 있습니까? 코드의 결과가 json 형식에서 성공했기 때문에. 하지만 여전히 "정의되지 않은 변수 : query_params"오류가 있습니다.정의되지 않은 변수 : query_params

이 json 형식의 안드로이드 프로그램에서 listview를 만들고 싶습니다. 내 안드로이드에서 내 comments.php에서 성공 json 형식을 표시 할 수 없습니다. 그리고 아마도,이 오류는 "정의되지 않은 변수 : query_params"때문일 수 있습니다.

enter image description here

이이 목록보기를 만들기 위해 comments.php에서 JSON 형식을 읽어 내 안드로이드 코딩 내 comments.php

<?php 


require("config.inc.php"); 

//initial query 
$query = "Select * FROM comments"; 

//execute query 
try { 
$stmt = $db->prepare($query); 
$result = $stmt->execute($query_params); 
} 
catch (PDOException $ex) { 
$response["success"] = 0; 
$response["message"] = "Database Error!"; 
die(json_encode($response)); 
} 


$rows = $stmt->fetchAll(); 


if ($rows) { 
$response["success"] = 1; 
$response["message"] = "Post Available!"; 
$response["posts"] = array(); 

foreach ($rows as $row) { 
    $post    = array(); 
    $post["post_id"] = $row["post_id"]; 
    $post["username"] = $row["username"]; 
    $post["title"] = $row["title"]; 
    $post["message"] = $row["message"]; 


    //update our repsonse JSON data 
    array_push($response["posts"], $post); 
} 

// echoing JSON response 
echo json_encode($response); 


} else { 
$response["success"] = 0; 
$response["message"] = "No Post Available!"; 
die(json_encode($response)); 
} 

?> 

입니다. listview에는 아무 것도 표시되지 않습니다.

당신이 변수가 포함되어야 무엇인지 정의하기 전에 execute() 문에서 변수 $query_params을 사용하려고하기 때문에 당신은 "정의되지 않은 변수"오류가 표시되는

import java.util.ArrayList; 
import java.util.HashMap; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import android.app.ListActivity; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 

public class ReadComments extends ListActivity { 


private ProgressDialog pDialog; 


private static final String READ_COMMENTS_URL = "http://192.168.56.101/webservice/comments.php"; 


private static final String TAG_SUCCESS = "success"; 
private static final String TAG_TITLE = "title"; 
private static final String TAG_POSTS = "posts"; 
private static final String TAG_POST_ID = "post_id"; 
private static final String TAG_USERNAME = "username"; 
private static final String TAG_MESSAGE = "message"; 

private JSONArray mComments = null; 
private ArrayList<HashMap<String, String>> mCommentList; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // note that use read_comments.xml instead of our single_post.xml 
    setContentView(R.layout.read_comments); 
} 

@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 
    // loading the comments via AsyncTask 
    new LoadComments().execute(); 
} 

public void addComment(View v) { 
    Intent i = new Intent(ReadComments.this, AddComment.class); 
    startActivity(i); 
} 


public void updateJSONdata() { 


    mCommentList = new ArrayList<HashMap<String, String>>(); 
    JSONParser jParser = new JSONParser(); 
    JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL); 

    try { 
     mComments = json.getJSONArray(TAG_POSTS); 
     for (int i = 0; i < mComments.length(); i++) { 
      JSONObject c = mComments.getJSONObject(i); 


      String title = c.getString(TAG_TITLE); 
      String content = c.getString(TAG_MESSAGE); 
      String username = c.getString(TAG_USERNAME); 


      HashMap<String, String> map = new HashMap<String, String>(); 

      map.put(TAG_TITLE, title); 
      map.put(TAG_MESSAGE, content); 
      map.put(TAG_USERNAME, username); 


      mCommentList.add(map); 


     } 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 


private void updateList() { 
    ListAdapter adapter = new SimpleAdapter(this, mCommentList, 
      R.layout.single_post, new String[] { TAG_TITLE, TAG_MESSAGE, 
        TAG_USERNAME }, new int[] { R.id.title, R.id.message, 
        R.id.username }); 

    setListAdapter(adapter); 
    ListView lv = getListView();  
    lv.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
     } 
    }); 
} 

public class LoadComments extends AsyncTask<Void, Void, Boolean> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(ReadComments.this); 
     pDialog.setMessage("Loading Comments..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(true); 
     pDialog.show(); 
    } 

    @Override 
    protected Boolean doInBackground(Void... arg0) { 
     updateJSONdata(); 
     return null; 

    } 

    @Override 
    protected void onPostExecute(Boolean result) { 
     super.onPostExecute(result); 
     pDialog.dismiss(); 
     updateList(); 
    } 
} 
} 
+0

이봐, 여기에 $ 행 [ "post_id를을"]; post_id 키의 값입니까? –

+0

누군가 MyBringBack의 자습서를 따라 갔다고 추측하십시오 : D – ymerdrengene

답변

2

ReadComments.java. this answer을 참조하십시오.

execute() 문으로 보내는 $query_params도 필요하지 않으므로 $query에서 매개 변수를 사용하지 않는 방법을 확인하십시오. this PHP manual on PDO Statement을 참조하십시오.

그래서

, 당신의 오류를 수정, 그냥이 변경 :

$result = $stmt->execute($query_params); 

을이 사람 :

$result = $stmt->execute(); 
+0

당신은 내 생명을 구했습니다! :)! 1 개 이상을 줄 수 있기를 바랍니다. – ymerdrengene

1

을 그래서, 당신의 오류를 수정, 그냥이 변경 :

$ 결과 = $ stmt-> execute ($ query_params);

대상 :

$ result = $ stmt-> execute();

내가 당신이 말한대로, 내가이

얻고 있음을 변경

{: "메시지"0 "성공"! "데이터베이스 오류"}