无法从我的数据库中获取数据,本地主机服务器
问题描述:
我在我的本地主机(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。以[]作为输出的开始,它是一个数组。所以在技术上你有一个带有Json对象数的1项的数组。
$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对象数组;
+0
我试过了,但没有得到结果,我的PHP网站有什么问题? –
你可以添加你的json结构吗? –
在浏览器上看起来像这样 [{“0”:“1”,“id”:“1”,“1”:“Hakob”,“name”:“Hakob”,“2”:“abc @ email .COM”, “电子邮件”: “[email protected]”},{ “0”: “2”, “ID”: “2”, “1”: “阿尔森”, “名称”: “阿尔森”, “2”:“[email protected]”,“email”:“[email protected]”}] –
使用retrofit和Gson =) – Natan