android的json无法连接到数据库
我试图将json数组的信息解析到android中。 我使用下面的代码,从Web服务获取信息,如果我打开php文件,一切正常,但在android中,我无法连接到数据库。我确实设置了访问internet的权限 以下是我使用的代码:android的json无法连接到数据库,android,json,web-services,Android,Json,Web Services,我试图将json数组的信息解析到android中。 我使用下面的代码,从Web服务获取信息,如果我打开php文件,一切正常,但在android中,我无法连接到数据库。我确实设置了访问internet的权限 以下是我使用的代码: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.la
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
StrictMode.enableDefaults(); //STRICT MODE ENABLED
resultView = (TextView) findViewById(R.id.result);
getData();
}
public void getData(){
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.10.28/albana/getAllCustomers.php"); //YOUR PHP SCRIPT ADDRESS
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
isr = entity.getContent();
}
catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
resultView.setText("Couldnt connect to database");
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//parse json data
try {
String s = "";
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++){
JSONObject json = jArray.getJSONObject(i);
s = s +
"Name : "+json.getString("FirstName")+" "+json.getString("LastName")+"\n"+
"Age : "+json.getInt("Age")+"\n"+
"Mobile Using : "+json.getString("Mobile")+"\n\n";
}
resultView.setText(s);
} catch (Exception e) {
// TODO: handle exception
Log.e("log_tag", "Error Parsing Data "+e.toString());
}
}
@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
StrictMode.enableDefaults();//已启用严格模式
resultView=(TextView)findViewById(R.id.result);
getData();
}
public void getData(){
字符串结果=”;
InputStream isr=null;
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://192.168.10.28/albana/getAllCustomers.php“”;//您的PHP脚本地址
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
isr=entity.getContent();
}
捕获(例外e){
e(“Log_标记”,“http连接错误”+e.toString());
resultView.setText(“无法连接到数据库”);
}
//将响应转换为字符串
试一试{
BufferedReader读取器=新的BufferedReader(新的InputStreamReader(isr,“iso-8859-1”),8);
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
isr.close();
结果=sb.toString();
}
捕获(例外e){
Log.e(“Log_标记”,“错误转换结果”+e.toString());
}
//解析json数据
试一试{
字符串s=“”;
JSONArray jArray=新JSONArray(结果);
对于(int i=0;i您正在尝试连接到192.168.10.28,这是一个专用或不可路由的网络地址。您只能在该网络(或专门连接到该网络的网络)上连接到它。您是否通过wifi连接到网络?如果是,我希望它可以工作,因此请尝试在web浏览器中打开URL,看看是否收到json响应。如果您通过移动网络连接到它,我不希望它工作。我很确定您不想在此处使用HttpPost
。请尝试HttpGet
,语法保持不变。
还要检查Dave的答案。在httpd.conf文件中进行以下更改
更改:
全盘否定
允许从127.0.0.1开始
允许来自::1
允许从本地主机
致:
通融
然后重新启动所有服务,效果非常好。日志行Log.e(“Log_标记”,“http连接出错”+e.toString())中的LogCat中必须有一条错误语句;当然,url打开得很好,我知道它在网络上:)我不是说它不在网络上:-)我想知道它是否可以从你的手机上访问。私人网络不能从互联网上访问,所以如果你在你的移动网络上,这是有意义的。所以,在你的手机上,当你打开浏览器并转到192.168.10.28/albana/getAllCustomers.php时,你会得到json响应,但是t当您使用应用程序(使用“get”而不是“post”时,它没有得到响应。您是否在模拟器中尝试了它以查看会发生什么?如果您从LogCat发布错误,将更容易提供帮助。正如我提到的,必须有一个。