我的日期库有两列(id,name),位于localhost:88。我试图从我的Android应用程序进入它,但在解析Json时,应用程序提供了JSONException。我试过从没有Json解析的php获取数据,只是一个String。但它只获得NULL。 这是我的PHP脚本:
<?php
$con = mysqli_connect('127.0.0.1:3306', 'root', '', 'test');
if(mysqli_connect_errno())
echo 'Failed to connect to MySQL: ' . mysqli_connect_error();
$query = mysqli_query($con, "SELECT * FROM example");
$rows = array();
while($r = mysqli_fetch_assoc($query))
$rows[] = $r;
print(json_encode($rows));
mysqli_close($con);
?>
这是我的方法:
public class MainActivity extends Activity
{
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
txt = (TextView) findViewById(R.id.textView);
txt.setText(getDataFromDB());
}
public String getDataFromDB()
{
try
{
HttpPost httpPost = new HttpPost("http://127.0.0.1:88/basicphp/GetData.php");
HttpClient httpClient = new DefaultHttpClient();
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpClient.execute(httpPost, responseHandler);
return response.trim();
}
catch(Exception e){
return "error\n" + e.getMessage();
}
}
}
当我输入我的浏览器&#34; http://localhost:88/basicphp/GetData.php
&#34;时,我明白了:&#34; [{&#34; id&#34;:&#34; 1&#34; &#34;名称&#34;:&#34;约翰&#34;}]&#34 ;.所以我想在我的TextView上至少得到这个字符串,但TextView只得到null :(那么,我做错了什么?
答案 0 :(得分:1)
更改
HttpPost httpPost = new HttpPost("http://127.0.0.1:88/basicphp/GetData.php");
它到
HttpPost httpPost = new HttpPost("http://10.0.2.2/basicphp/GetData.php");
访问[问题]:java.net.ConnectException: /127.0.0.1:8080 an android emulator