Android - 使用不同IP的JSON连接

时间:2012-04-02 14:02:56

标签: android

我正在开发一个Android应用程序,用我的手机中的PHP通过PHP从MySQL获取数据。

在此之前,我已经写了一个JSON连接,以便从Web服务器获取数据成功。

现在我已经在我的电脑上安装了php和MySQL服务器,然后我希望用我的IP地址从中获取数据,但它不起作用并抛出异常。

Error parsing data org.json.JSONException: Value 404 of type java.lang.Integer cannot be converted to JSONArray

但是,我让手机连接到带有wifi的路由器,并将JSON连接IP更改为192.168.XXX.XXX,它成功获取数据。

我可以知道会发生什么事吗?我的路由器配置错了吗?还是php主机?

(我使用WAMP来托管php服务器)

封闭代码:

这是我的JSON代码:

String result = "";
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("sql",
            "select * from staff_table"));
    InputStream is = null;
    // http post
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(
                "http://my-ip/android_getdata.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection " + e.toString());
    }
    // convert response to string
    try {
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();

        result = sb.toString();
    } catch (Exception e) {
        Log.e("log_tag", "Error converting result " + e.toString());
    }

    // parse json data
    try {

        JSONArray jArray = new JSONArray(result);
        for (int i = 0; i < jArray.length(); i++) {
            JSONObject json_data = jArray.getJSONObject(i);
            //do some here
        }
    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());
    }

0 个答案:

没有答案