1

我收到此错误Error parsing <!DOCTYPE HTML...,就像它没有找到资源一样,但我传递了正确的 url:

“http://10.0.2.2/portablevision/webservice.php”;

无论如何,这是我的 PHP 网络服务:

<?php
    $response = array();

    require_once __DIR__ . '/db_connect.php';

    $db = new DB_CONNECT();

    $result = mysql_query("SELECT * FROM words") or die(mysql_error());

    if(mysql_num_rows($result) > 0){
        $response["words"] = array();

        while($row = mysql_fetch_array($result)){
            $word = array();
            $word["id"] = $row["id"];
            $word["word"] = $row["word"];

            array_push($response["words"], $word);
        }

        $response["success"] = 1;

        echo json_encode($response);
    }
    else{
        $response["success"] = 0;
        $response["message"] = "No word found";

        echo json_encode($response);
    }
?>

安卓活动:

public class MainActivity extends Activity{
    JSONParser jParser = new JSONParser();

    ArrayList<HashMap<String, String>> wordsList;

    private static String url_webservice = "http://10.0.2.2/portablevision/webservice.php";
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_WORDS = "words";

    JSONArray palavras = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        wordsList = new ArrayList<HashMap<String,String>>();

        List<NameValuePair> params = new ArrayList<NameValuePair>();

        JSONObject json = jParser.makeHttpRequest(url_webservice, "GET", params);

        Log.d("All words", json.toString());

        try{
            int success = json.getInt(TAG_SUCCESS);

            if(success == 1){
                words = json.getJSONArray(TAG_WORDS);

                for(int i = 0; i < words.length; i++){
                    JSONObject c = words.getJSONObject(i);

                    String id = c.getString("id");
                    String word = c.getString("word");

                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put("id", id);
                    map.put("word", word);

                    wordsList.add(map);
                }
            }
            else{
                Toast.makeText(MainActivity.this, "Unable to load words", Toast.LENGTH_LONG).show();
            }
        }
        catch(JSONException e){
            Toast.makeText(MainActivity.this, "Erro: " + e.getMessage(), Toast.LENGTH_LONG).show();
        }
}

JSONParser 类:

public class JSONParser {
    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    public JSONParser(){}

    public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params){
        try{
            if(method == "POST"){
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
             }
             else if(method == "GET"){
                 DefaultHttpClient httpClient = new DefaultHttpClient();
                 String paramString = URLEncodedUtils.format(params, "utf-8");
                 url += "?" + paramString;
                 HttpGet httpGet = new HttpGet(url);

                 HttpResponse httpResponse = httpClient.execute(httpGet);
                 HttpEntity httpEntity = httpResponse.getEntity();
                 is = httpEntity.getContent();
             }
         }
         catch(UnsupportedEncodingException e){
             e.printStackTrace();
         }
         catch(ClientProtocolException e){
             e.printStackTrace();
         }
         catch(IOException e){
             e.printStackTrace();
         }

         try{
             BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);
             StringBuilder sb = new StringBuilder();
             String line = null;
             while((line = reader.readLine()) != null){
                 sb.append(line + "\n");
             }

             is.close();
             json = sb.toString();
         }
         catch(Exception e){
             Log.e("Buffer error", "Error converting result " + e.toString());
         }

         try{
             jObj = new JSONObject(json);
         }
         catch(JSONException e){
             Log.e("JSON Parser", "Error parsing data " + json.toString());
         }

         return jObj;
    }
}

JSON的结果是正确的:

{"words":[{"id":"1","word":"correct place"}],"success":1}

欢迎任何帮助

谢谢


您不能只为对象添加“属性”——对象的概念完全在编译时完成。在运行时,CPU 不知道给定的内存块称为“对象”或您决定调用类实例的任何内容。

(这本身就很有趣,因为 C++ 没有属性)

如果您只想要一个按名称引用的数据字段,那么任何标准关联容器都应该满足您的需求。

4

0 回答 0