我正在制作一个应用程序,它在 listView 中显示在线收音机列表,该列表是通过在线 MySql 数据库获得的。我在所有版本的模拟器中测试了我的应用程序,它运行良好。但在设备中,应用程序终止。当我通过 USB 连接时不会关闭应用程序,但不会加载 Json 内容。
通过 USB 调试连接的设备不会为收音机充电
日志:
03-01 17:35:05.109: E/Buffer Error(2273): Error converting result java.lang.NullPointerException
03-01 17:35:05.109: E/JSON Parser(2273): Error parsing data org.json.JSONException: End of input at character 0 of
下面是代码:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class ListaRadios extends ListActivity {
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> listaradios;
private static String url_radios = "Url to php mysql connection with";
private static final String TAG_SUCESSO = "sucesso";
private static final String TAG_RADIOS = "radios";
private static final String TAG_ID = "id";
private static final String TAG_NOME = "nome";
JSONArray radios = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lista_radios);
listaradios = new ArrayList<HashMap<String, String>>();
new LoadAllRadios().execute();
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position, long id_item) {
String id = ((TextView) view.findViewById(R.id.id)).getText().toString();
Intent in = new Intent(getApplicationContext(), Player.class);
in.putExtra(TAG_ID, id);
startActivityForResult(in, 100);
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
private void direciona(Class<?> menu){
Intent myIntent = new Intent(this, menu);
startActivity(myIntent);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.contato:
direciona(Contato.class);
return true;
case R.id.sugerir:
direciona(Sugerir.class);
return true;
case R.id.listar:
direciona(ListaRadios.class);
return true;
case R.id.player:
direciona(Player.class);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == 100) {
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
class LoadAllRadios extends AsyncTask<String, String, String> {
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ListaRadios.this);
pDialog.setMessage("Buscando rádios. Aguarde...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(url_radios, "GET", params);
Log.d("Todas as rádios: ", json.toString());
try {
// Checking for SUCCESS TAG
int sucesso = json.getInt(TAG_SUCESSO);
if (sucesso == 1) {
radios = json.getJSONArray(TAG_RADIOS);
for (int i = 0; i < radios.length(); i++) {
JSONObject c = radios.getJSONObject(i);
String id = c.getString(TAG_ID);
String nome = c.getString(TAG_NOME);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_NOME, nome);
listaradios.add(map);
}
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
ListAdapter adapter = new SimpleAdapter(ListaRadios.this,
listaradios, R.layout.lista_item_radios,
new String[] {TAG_ID, TAG_NOME},
new int[] {R.id.id, R.id.nome});
setListAdapter(adapter);
}
});
}
}
}
php代码:
<?php
$resposta = array();
require_once 'db_connect.php';
$db = new DB_CONNECT();
$resultado = mysql_query("SELECT * FROM radios") or die(mysql_error());
if (mysql_num_rows($resultado) > 0) {
$resposta["radios"] = array();
while ($row = mysql_fetch_array($resultado)) {
$radio = array();
$radio["id"] = $row["id"];
$radio["nome"] = $row["nome"];
$radio["site"] = $row["site"];
$radio["imagem"] = $row["imagem"];
$radio["url_streaming"] = $row["url_streaming"];
array_push($resposta["radios"], $radio);
}
$resposta["sucesso"] = 1;
echo json_encode($resposta);
} else {
$resposta["sucesso"] = 0;
$resposta["mensagem"] = "Não foram encontradas rádios.";
echo json_encode($resposta);
}
?>
会是什么?
如果我运行模拟器,则输出如下:
03-01 18:41:40.047: D/Todas as rádios:(594): {"sucesso":1,"radios":[{"site":"www.radiomiriam.com.br","id":"1","url_streaming":"http:\/\/69.64.48.96:9880\/","nome":"Radio Miriam","imagem":""},{"site":"www.clarimdopampa.com.br","id":"2","url_streaming":"http:\/\/70.36.96.25:9928\/","nome":"Radio Clarim do Pampa","imagem":""},{"site":"","id":"5","url_streaming":"http:\/\/189.76.158.139:50004\/","nome":"Itapema FM - RS","imagem":""}]}
但是如果我运行 usb 调试输出并没有列出这个:
03-01 18:48:21.319: I/System.out(3717): Debugger has connected
03-01 18:48:21.319: I/System.out(3717): waiting for debugger to settle...
03-01 18:48:21.519: I/System.out(3717): waiting for debugger to settle...
03-01 18:48:21.719: I/System.out(3717): waiting for debugger to settle...
03-01 18:48:21.919: I/System.out(3717): waiting for debugger to settle...
03-01 18:48:22.119: I/System.out(3717): waiting for debugger to settle...
03-01 18:48:22.319: I/System.out(3717): waiting for debugger to settle...
03-01 18:48:22.519: I/System.out(3717): waiting for debugger to settle...
03-01 18:48:22.719: I/System.out(3717): waiting for debugger to settle...
03-01 18:48:22.919: I/System.out(3717): debugger has settled (1475)
03-01 18:48:23.699: W/System.err(3717): java.net.UnknownHostException: nsi.inf.br
03-01 18:48:23.709: W/System.err(3717): at java.net.InetAddress.lookupHostByName(InetAddress.java:508)
03-01 18:48:23.709: W/System.err(3717): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:296)
03-01 18:48:23.719: W/System.err(3717): at java.net.InetAddress.getAllByName(InetAddress.java:258)
03-01 18:48:23.719: W/System.err(3717): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
03-01 18:48:23.719: W/System.err(3717): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
03-01 18:48:23.729: W/System.err(3717): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
03-01 18:48:23.739: W/System.err(3717): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
03-01 18:48:23.739: W/System.err(3717): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
03-01 18:48:23.749: W/System.err(3717): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
03-01 18:48:23.749: W/System.err(3717): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
03-01 18:48:23.749: W/System.err(3717): at com.nsistations.JSONParser.makeHttpRequest(JSONParser.java:62)
03-01 18:48:23.749: W/System.err(3717): at com.nsistations.ListaRadios$LoadAllRadios.doInBackground(ListaRadios.java:120)
03-01 18:48:23.749: W/System.err(3717): at com.nsistations.ListaRadios$LoadAllRadios.doInBackground(ListaRadios.java:1)
03-01 18:48:23.749: W/System.err(3717): at android.os.AsyncTask$2.call(AsyncTask.java:185)
03-01 18:48:23.749: W/System.err(3717): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
03-01 18:48:23.749: W/System.err(3717): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
03-01 18:48:23.749: W/System.err(3717): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
03-01 18:48:23.759: W/System.err(3717): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
03-01 18:48:23.759: W/System.err(3717): at java.lang.Thread.run(Thread.java:1019)
03-01 18:48:23.759: E/Buffer Error(3717): Error converting result java.lang.NullPointerException
03-01 18:48:23.759: E/JSON Parser(3717): Error parsing data org.json.JSONException: End of input at character 0 of