【发布时间】:2016-03-30 01:50:31
【问题描述】:
我无法从 Android 上的 JSON 获取数据。
我的PHP如下:
$user_id=$_REQUEST['user_id'];
$r=mysql_query("select * from tbl_storefinder_pedidos where user_id='$user_id'",$con);
if( mysql_num_rows( $r ) > 0 ) {
while($row = mysql_fetch_array($r))
{
$flag[pedidos_id]=$row[pedidos_id];
$flag[pedidos_nome]=$row[pedidos_nome];
$flag[pedidos_email]=$row[pedidos_email];
print(json_encode($flag));
}
}
mysql_close($con);
当我尝试在 Android 中获取数据时,只能捕获第一条记录。
关注我的活动:
class select extends AsyncTask<String, Integer, String> {
private StringBuilder sb;
private ProgressDialog pr;
private HttpResponse req;
private InputStream is;
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(getApplicationContext(), "Captando ...", Toast.LENGTH_LONG).show();
}
@Override
protected String doInBackground(String... arg0) {
UserAccessSession userAccess = UserAccessSession.getInstance(FinalizarPedido.this);
UserSession userSession = userAccess.getUserSession();
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("user_id", String.valueOf(userSession.getUser_id())));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://marketingdigitalabc.com.br/buysell/pedidos_show.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("pass 1", "connection success ");
} catch (Exception e) {
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address",
Toast.LENGTH_LONG).show();
}
try {
BufferedReader reader = new BufferedReader
(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.e("pass 2", "connection success ");
} catch (Exception e) {
Log.e("Fail 2", e.toString());
}
try {
JSONObject json_data = new JSONObject(result);
for (int i = 0; i < 5; i++) {
pedidos_nome = (json_data.getString("pedidos_nome"));
pedidos_id = (json_data.getString("pedidos_id"));
pedidos_email = (json_data.getString("pedidos_email"));
}
} catch (Exception e) {
Log.e("Fail 3", e.toString());
}
return user_id;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
String[] lista = {"Produtos:" +pedidos_id};
final ListView listaPedido = (ListView) findViewById(R.id.lista);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(FinalizarPedido.this, android.R.layout.simple_list_item_1, lista);
listaPedido.setAdapter(adapter);
listaPedido.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> lista, View item, int posicao, long id) {
listaPedido.getItemAtPosition(posicao);
AlertDialog.Builder builder = new AlertDialog.Builder(FinalizarPedido.this);//Cria o gerador do AlertDialog
builder.setTitle("Pedido: " + pedidos_nome);
builder.setMessage("Pedido id: " + pedidos_id + "\nPedido E-mail: " + pedidos_email+ "\nPedido Nome: " + pedidos_nome);
builder.setNegativeButton("Fechar", null);
alerta = builder.create();
alerta.show();
}
});
}
我的目标是列出通过 JSON 接收的数据。有人可以帮帮我吗?
【问题讨论】:
-
英文问题
-
编辑过对我有帮助吗?
-
尝试使用“EntityUtils.toString(entity)”而不是“entity.getContent()”
-
我已投票决定重新开放。同时,虽然和你的问题无关,don't use the
mysql_*API! -
我使用了 EntityUtils.toString (entity) 但还无法获取所有的 json 数据。 :(
标签: java php android mysql json