2017-08-07 153 views
0

我在我的本地主機(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); 
?> 
+0

你可以添加你的json結構嗎? –

+0

在瀏覽器上看起來像這樣 [{「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]」}] –

+0

使用retrofit和Gson =) – Natan

回答

0

看看這個方法得到的數據作爲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/

和教程在這裏

http://www.vogella.com/tutorials/Retrofit/article.html

0

您沒有一個有效的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網站有什麼問題? –

相關問題