无法解析phpmyadmin中的数据

时间:2017-07-25 14:29:28

标签: java php android mysql phpmyadmin

我试图在我的Android应用程序上从phpmyadmin检索数据,但首先我想通过logcat测试它是否有效。当我测试它时,我不会找回名字遗憾的是数据库中的用户。

这是我的ShowUsers类:

package ie.example.artur.adminapp;

import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toolbar;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;

import android.view.View;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

/**
 * Created by family on 24/07/2017.
 */

public class ShowUsers extends AppCompatActivity {


    ListView lv;
        String[] names = {"Amy","John","Joseph","Carl"};
        InputStream is = null;
    String line= null;
    String result = null;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.showusers);

        lv = (ListView) findViewById(R.id.lv);

        //Inlfate the list view with the items

        lv.setAdapter(new ArrayAdapter<String>(ShowUsers.this,android.R.layout.simple_list_item_1,names));

        android.widget.Toolbar toolbar = (android.widget.Toolbar) findViewById(R.id.toolbar);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        //set up the code to fetch data from the database


        try {
            HttpClient httpClient = new DefaultHttpClient();

            HttpPost httpPost = new HttpPost("http://10.3.2.51/tut.php");

            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            //SETUP THE INPUTSTREAM TO RECEIVE THE DATA (INITIAL)
        }catch (Exception e){
            System.out.println("Exception 1 caught");
        }

        try {

            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            // Create a String builder object to hold the data
            StringBuilder sb = new StringBuilder();
            while((line = reader.readLine())!=null)
                sb.append(line+"\n");


            //Use the toString() method to get the data in the result

            result = sb.toString();
                    is.close();
            //check the data by printing the results in the logcat

            System.out.println("-----Here is my data -----");
            System.out.println(result);

        }catch(Exception e){
            System.out.print("Exception 2 caught");
        }



    }
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        {
            switch (item.getItemId())
            {
                case R.id.action_settings : startActivity (new Intent(this, ShowUsers.class));
                    break;
            }
            return super.onOptionsItemSelected(item);
        }}




}

这是tut.php文件的设置,位置为C:\xampp\www

<?php

$con=mysql_connect("localhost","root","");
mysql_select_db("socialmedia_website",$con);

$r=mysql_query("select name from users where 1";

while($row=mysql_fetch_array($r))

    {

        $out[]=$row;
    }

    print(json_encode($out));
    mysql_close($con)

phpyadmin中的表:

enter image description here

这是目前输出的内容:

enter image description here

它应该从数据库中输出用户

0 个答案:

没有答案