解析时出错

时间:2012-10-27 22:48:15

标签: php android web-services

我收到此错误Error parsing <!DOCTYPE HTML...,就像找不到资源一样,但我传递的是正确的网址:

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

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}

欢迎任何帮助

由于

0 个答案:

没有答案