我在主机中使用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;
}
}
}
php与json得到这个
{&#34; cities&#34;:[{&#34; name&#34;:&#34; aitor&#34;}]}
和logcat:
12-02 04:24:01.492: E/WebService(2190): End of input at character 0 of
感谢
答案 0 :(得分:1)
你在MainActivity中所以行
HttpResponse execute = cliente.execute(httpget);
是投掷networkonmainthreadexception你可以使用(在第一次尝试捕获)中检查它
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);
}
}
}