[英]Can't fetch data from my database,localhost server
我在本地主机(XAMPP)上有数据库,我正在制作可从数据库中获取数据的应用程序。 我可以在浏览器中看到我的数据库,但是在我的android设备上看不到。 您能帮忙吗:我已经添加了互联网许可
这是我的代码:
public class MainActivity extends AppCompatActivity {
String JSON_STRING;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void getJSON(View view){
new BackgroundTask().execute();
}
class BackgroundTask extends AsyncTask{
String json_url;
@Override
protected void onPreExecute() {
json_url="http://10.0.2.2/ContactDB/readdata.php";
}
@Override
protected String doInBackground(Object[] params) {
try {
URL url=new URL(json_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
InputStream inputStream=httpURLConnection.getInputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder=new StringBuilder();
while((JSON_STRING=bufferedReader.readLine())!=null){
stringBuilder.append(JSON_STRING+"\n");
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return stringBuilder.toString().trim();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Object[] values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Object o) {
TextView textView = (TextView) findViewById(R.id.textview);
textView.setText((CharSequence) o);
}
}
}
这是我的PHP服务器部分,它工作正常,我认为JSON提取部分有问题。
<?php
$servername = "127.0.0.1";
$username = "root";
$password = "12345";
$dbname = "contactsdb";
// Create connection
$connect = mysqli_connect($servername,$username,$password,$dbname);
if ($connect === false){
die ("Error:Couldn't connect");
}
$sql = "SELECT * FROM contacts";
$result = mysqli_query($connect, $sql);
$response = array();
while($row = mysqli_fetch_array($result)){
$output[]=$row;
}
print json_encode($output);
// Close connection
mysqli_close($connect);
?>
查看此方法以获取数据作为GET请求
private static String makeHttpRequest(URL url) throws IOException {
String jsonResponse = "";
if (url == null) {
return jsonResponse;
}
HttpsURLConnection urlConnection = null;
InputStream inputStream = null;
try {
//set up the connection
urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setReadTimeout(10000);
urlConnection.setConnectTimeout(15000);
urlConnection.setRequestMethod("GET");
//connect
urlConnection.connect();
//receive DataSend if the response code is ok
if (urlConnection.getResponseCode() == 200) {
inputStream = urlConnection.getInputStream();
jsonResponse = readFromStream(inputStream);
}
} catch (IOException e) {
Log.e(LOG_TAG, "Problem retrieving the JSON results.", e);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (inputStream != null) {
inputStream.close();
}
}
return jsonResponse;
}
和readFromStream方法
/**
* Convert the {@link InputStream} into a String Which Contains the
* whole JSON response from the server
*/
private static String readFromStream(InputStream inputStream) throws IOException {
StringBuilder output = new StringBuilder();
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream,
Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader(inputStreamReader);
String line = reader.readLine();
while (line != null) {
output.append(line);
line = reader.readLine();
}
}
return output.toString();
}
建立网址
/**
* create a url from string
*/
private static URL createUrl(String StringUrl) {
URL url = null;
try {
url = new URL(StringUrl);
} catch (MalformedURLException e) {
Log.e(LOG_TAG, "Problem building the URL ", e);
}
return url;
}
但是最好使用改造库来获取数据,您可以在此处找到文档
http://square.github.io/retrofit/
还有这里的教程
您没有有效的json。 以[]作为输出的开始是一个数组。 因此,从技术上讲,您有一个包含1个项的数组,其编号为Json Object。
$response = array(); <-- Un-used object. Why do you have an $output[]? Where did you declare it?
while($row = mysqli_fetch_array($result)){
$response[]=$row;
}
print json_encode($response);
然后,您应该有一个正确的json对象数组;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.