【问题标题】:php Json getting a blank textviewphp Json得到一个空白的textview
【发布时间】:2014-12-02 04:39:43
【问题描述】:

我在主机中使用 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得到这个

{"城市":[{"name":"aitor"}]}

和日志猫:

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

谢谢

【问题讨论】:

标签: android mysql json


【解决方案1】:

你在 MainActivity 所以这条线

HttpResponse execute = cliente.execute(httpget);

是 Throwing networkonmainthreadexception 你可以通过使用来检查它(在第一次尝试 catch 中也是如此)

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

所以使用 AsyncTask 而不是使用 AsyncTaskLoader 更好。

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);
    }
}

}

【讨论】:

  • 非常感谢!!我必须做到!但是,我可以从数据库中填充更多数据的对象吗?
  • 我可以用不同的数据逐行填充列表视图吗?像这样:姓名和年龄_______姓名和年龄
猜你喜欢
  • 2015-08-13
  • 2010-11-15
  • 1970-01-01
  • 1970-01-01
  • 2019-06-27
  • 1970-01-01
  • 1970-01-01
  • 2011-12-05
  • 1970-01-01
相关资源
最近更新 更多