gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:48:14 26 4
gpt4 key购买 nike

我正在制作一个应用程序,它在 listView 中显示在线电台列表,该列表是通过在线 MySql 数据库获得的。我在所有版本的模拟器中测试了我的应用程序,它运行良好。但在设备中,应用程序终止。当我通过 usb 连接时,应用程序没有关闭,但没有加载 Json 内容。

通过usb调试连接的设备不会对 radio 充电

日志:

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

最佳答案

java.net.UnknownHostException: nsi.inf.br

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

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

关于android - 仅在真实设备上出错。适用于所有模拟器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15166591/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com