android - php Json 得到一个空白的 TextView

标签 android mysql json

我在主机中使用 mysql 数据库,我想从我的主机接收数据,但是当我将数据获取到 TextView 时,它是空白的。

public class MainActivity extends Activity {
TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv1=(TextView)findViewById(R.id.name);
    Loader loader = new Loader(getApplicationContext());
    tv1.setText(loader.loadInBackground());
}
public static class Loader extends AsyncTaskLoader<String>{
    public Loader (Context contexto)
    {
        super(contexto);

    }

    @Override
    public String loadInBackground() {

        String resultado = "";
        String entrada = "";

        DefaultHttpClient cliente = new DefaultHttpClient();
        HttpGet httpget = new HttpGet("http://comupunt.esy.es/cities.php");
        try {
            HttpResponse execute = cliente.execute(httpget);
            InputStream contenido = execute.getEntity().getContent();
            BufferedReader buffer = new BufferedReader(new InputStreamReader(contenido)); 

            String s="";
            while((s=buffer.readLine())!=null)
                resultado+=s;
            {

            }
        } catch (Exception e) {
            // TODO: handle exception
        }

        try {

            JSONObject object = new JSONObject(resultado);
            JSONArray jarray = object.getJSONArray("cities");

            for (int i=0;i<jarray.length();i++)
            {
                JSONObject jobject = jarray.getJSONObject(i);
                String name=jobject.getString("name");
                entrada += name;

            }


        } catch (Exception e) {
            // TODO: handle exception
            Log.e("WebService", e.getMessage());
        }

        return entrada;

    }
 }


 }

带有 json 的 php 得到这个

{"城市":[{"名称":"aitor"}]}

和 logcat:

12-02 04:24:01.492: E/WebService(2190): End of input at character 0 of 

谢谢

最佳答案

你在 MainActivity 所以行

HttpResponse execute = cliente.execute(httpget);

是 Throwing networkonmainthreadexception 你可以通过使用检查它(在第一次 try catch 时也是如此)

catch (Exception e) {
        // TODO: handle exception
         Log.e("errormsg", e.ToString();
    }

因此,与其使用 AsyncTaskLoader,不如使用 AsyncTask。

public class MainActivity extends Activity {

TextView tv1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv1 = (TextView) findViewById(R.id.text);
    Exe exe = new Exe();
    try {
        URI uri = new URI("http://comupunt.esy.es/cities.php");
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    exe.execute();

}

class Exe extends AsyncTask<URL, String, String> {

    String entrada = "";

    @Override
    protected String doInBackground(URL... url) {

        String resultado = "";

        try {
            DefaultHttpClient cliente = new DefaultHttpClient();

            DefaultHttpClient httpClient = new DefaultHttpClient();
            URI uri = new URI("http://comupunt.esy.es/cities.php");
            HttpGet http = new HttpGet(uri);


            HttpResponse execute = cliente.execute(http);

            InputStream contenido = execute.getEntity().getContent();

            BufferedReader buffer = new BufferedReader(
                    new InputStreamReader(contenido));

            String s = "";

            while ((s = buffer.readLine()) != null) {
                resultado += s;
            }
            JSONObject object = new JSONObject(resultado);
            Log.e("WebService1", object.toString());
            JSONArray jarray = object.getJSONArray("cities");

            for (int i = 0; i < jarray.length(); i++) {
                JSONObject jobject = jarray.getJSONObject(i);
                String name = jobject.getString("name");
                entrada = name;

            }

        } catch (Exception e) {
            // TODO: handle exception
            Log.e("WebService", e.getMessage());
        }

        return entrada;

    }


    @Override
    protected void onPostExecute(String res) {

        tv1.setText(entrada);
    }
}

关于android - php Json 得到一个空白的 TextView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27241977/

相关文章:

android - 廉价的支持 Wi-Fi 的 Android 或其他可编程设备

android - 在 RecyclerView 适配器中使用自定义 View ?

php - 从 SQL 数据库中搜索并显示结果

php - 将名称插入数据库?

c# - 在 C# WPF 中使用 MySQL 数据库使 Datagrid 可搜索

android - 将现有项目文件复制到新的 AndroidStudio Android 项目后出现错误

c# - 在 Diffusion 中更新 JSON 主题(Unified .NET Client Api)

javascript - jquery getJSON 调用出现问题

javascript - 使用 if/else 语句将真/假显示为是/否 jquery

java - Android/Java 循环无法正常工作