Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Android-与Web服务器/MySQL的JSON连接_Php_Android_Mysql_Json - Fatal编程技术网

Php Android-与Web服务器/MySQL的JSON连接

Php Android-与Web服务器/MySQL的JSON连接,php,android,mysql,json,Php,Android,Mysql,Json,我是在我的第一个android编程的日子,我被困在这 我正在尝试通过android项目中的PHP与MySQL数据库建立连接(登录页面)。我已经成功地在模拟器中使用了我网络中的主机ip地址。然而,我似乎无法连接到外部IP,这是fulcral,因为我的意思是在实际设备上实现它 在设备上,由于它在网络之外,我无法以任何方式连接 连接失败的结果是崩溃,LogCat停留在按钮-登录 我已将Internet权限添加到清单中 成功连接和不成功连接之间的唯一变化是地址(从内部到外部) //JSON解析器代码:

我是在我的第一个android编程的日子,我被困在这

我正在尝试通过android项目中的PHP与MySQL数据库建立连接(登录页面)。我已经成功地在模拟器中使用了我网络中的主机ip地址。然而,我似乎无法连接到外部IP,这是fulcral,因为我的意思是在实际设备上实现它

在设备上,由于它在网络之外,我无法以任何方式连接

连接失败的结果是崩溃,LogCat停留在按钮-登录

我已将Internet权限添加到清单中

成功连接和不成功连接之间的唯一变化是地址(从内部到外部)

//JSON解析器代码:
公共JSONObject getJSONFromUrl(字符串url,列表参数){
//发出HTTP请求
试一试{
//defaultHttpClient
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
setEntity(新的UrlEncodedFormEntity(参数));
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
is,“iso-8859-1”),8);
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
json=sb.toString();
Log.e(“JSON”,JSON);
}捕获(例外e){
Log.e(“缓冲区错误”,“错误转换结果”+e.toString());
}
//尝试将字符串解析为JSON对象
试一试{
jObj=新的JSONObject(json);
}捕获(JSONException e){
Log.e(“JSON解析器”,“错误解析数据”+e.toString());
}
//返回JSON字符串
返回jObj;
}
//创建JSON
JSONObject json=jsonParser.getJSONFromUrl(loginURL,params);

如果您使用真实设备在同一WiFi网络中运行本地服务器,请使用ipconfig(Windows)检查PC服务器的IP,它可能类似于192.168.100.101或类似版本。因此,您只需从设备连接到此地址。

首先,感谢您的回答。但是,我没有WiFi网络设置。我有一个外部IP激活的虚拟机(目前托管web服务),我希望通过PHP和Internet将任何android设备连接到它(MySQL)。它应该和emulator中的intranet IP一样工作,对吗?@SilentFreak让我澄清一下-您的PHP服务器正在运行,并且可以从web上看到,对吗?是的,我有一个服务器在web上运行,配置为XAMPP/Apache/MySQL。我也可以通过网络访问它,唯一能让它工作的方法就是通过模拟器上的内联网。只需将IP更改为web,应用程序在按下按钮连接/查询后就会崩溃。好的,无论如何-您可以通过添加与崩溃相关的LogCat日志来更新您的问题吗?此外,您说服务器可以从web上看到,但是“在设备上,因为它在网络之外,所以我无法以任何方式连接。”网络之外是什么意思?服务器通常不能从web上看到,只能从您的本地网络上看到?在这种情况下,您需要找到服务器的ip,并在android应用程序中对其进行硬编码
//JSON parser code:

public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {

    // Making HTTP request
    try {

        // defaultHttpClient
        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();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    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();
        json = sb.toString();
        Log.e("JSON", json);
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);            
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String

    return jObj;

}


//Creating the JSON

JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);