仅在真实设备上出错。适用于所有模拟器

时间:2013-03-01 20:46:15

标签: android

我正在创建一个应用程序,在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 

1 个答案:

答案 0 :(得分:2)

java.net.UnknownHostException: nsi.inf.br

您的设备无法访问nsi.inf.br,但仿真器(即您的工作站)可以。

再次检查工作站设置的设备连接设置。检查它们是否在同一网络上以及是否存在代理/防火墙问题。