我在一个主机使用mysql数据库,我想从我的主机recive数据,但是当我得到的数据到一个textview,它是空白的。php json得到一个空白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”}]}
和logcat的:
12-02 04:24:01.492: E/WebService(2190): End of input at character 0 of
感谢
的可能的复制http://stackoverflow.com/questions/24301521/jsonexception-end-of-input-at-character-0或http://stackoverflow.com/questions/ 23170610/error-parsing-data-org-json-jsonexception -in-input-at-character-0- – Chitrang 2014-12-02 05:10:56
我只在logcat中出错 – 2014-12-02 05:14:28