1

Error parsing <!DOCTYPE HTML...リソースが見つからないように、このエラーが発生していますが、正しいURLを渡しています:

"http://10.0.2.2/portablevision/webservice.php";

とにかく、ここに私のPHP Webサービスがあります:

<?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);
    }
?>

Android アクティビティ:

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}

どんな助けでも大歓迎です

ありがとう


シングルトンクラス - 本当に混乱

C++ で Singleton クラスを実装しようとしていますが、少し混乱しています。わかりました、次の2つのクラスがあるとしましょう:

class Animal {

public:
    virtual int age() = 0;
    virtual void breed() = 0;   
};


class Cat : public Animal {
  public:
Cat();
int age();
void breed();  
};                

このシステムに関連するその他のクラス .... (犬、魚など)

これで、1 つのオブジェクトだけを使用できるシングルトン クラスができました。

class Singleton 
{
  public:
    Animal *newAnimal(string theTypeOfAnimal);
  private:   
   static Animal* pinstance;
};                        

Animal *Singleton::newAnimal(string theTypeOfAnimal)
{
pinstance = new Cat;

}           

int main()
{        
Singleton *s;
return 0;
}      

アップデート:

新しいコード:

#include <iostream>
using namespace std;

class Animal {

public:
    virtual int age() = 0;
    virtual void breed() = 0;
};

class Cat : public Animal
{
public:
        virtual int age() { return 9; }
        virtual void breed() { }

 };
class Singleton
{
public:
    Animal *newAnimal(string theTypeOfAnimal);
  private:   
   static Animal* pinstance;
};

Animal* Singleton::pinstance = 0;


Animal *Singleton::newAnimal(string theTypeOfAnimal)
{
     this->pinstance = new Cat;
 return pinstance;
}  

 int main(int argc, char *argv[]) {

Singleton s;

Animal *myAnimal = NULL;

Animal *myAnimal = s->newAnimal("cat");

}

4

0 に答える 0