我如何从数组中的mysql中检索结果?

时间:2012-06-04 10:29:25

标签: java android mysql arrays json

我想将数据库中的结果作为数组获取。我有一张桌子“commande”,我有库存。我想把股票作为一个数组。这样的事情:array[0],array[],array[2] 这是我的表commande (idfichecmd idproduit idclt qte datecmd)

的结构

这是我的代码:

String response = null;
String res = "";
//the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("",));   
try {
    Log.i("","<<<<<<<<<<<<<<<<<<<<TRY");      
    response = CustomHttpClient.executeHttpPost("http://0.0.0.0/GetCmdDetails.php",nameValuePairs); 
    res=response.toString();
    // res = res.trim();
    //res= res.replaceAll("\\s+","");
    //error.setText(res);

} catch (Exception e) {
    Log.i("","<<<<<<<<<<<<<<<<<<<<Catch");
}

//parse json data
try{
    JSONArray jArray = new JSONArray(res);
    //-----------------------------------------
    //nom= new String[jArray.length()];
    //prenom= new String[jArray.length()];
    //----------------------------------------
    for (int i=0;i<jArray.length();i++) {
        JSONObject json_data = jArray.getJSONObject(i)

1 个答案:

答案 0 :(得分:0)

首先,测试您的查询是否给出了正确的结果。

我使用此解决方案来获取jsonarray:

使用HttpClient调用Web服务。

private List<NameValuePair> parameters;

    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(//yoururl);
    int statusCode = -1;
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(this.parameters));
        HttpResponse response = httpClient.execute(httpPost);
        statusCode = response.getStatusLine().getStatusCode();

 //Get your inputstream here and convert it to json after

    BufferedReader in = new BufferedReader
                        (new InputStreamReader(response.getEntity().getContent()));