首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android JSON获取特定列记录

Android JSON获取特定列记录
EN

Stack Overflow用户
提问于 2014-08-30 18:39:45
回答 1查看 530关注 0票数 0

我想使用JSON来获取特定列的记录,Parsing.Here是我的PHP MySql的屏幕截图,其中包含一些数据。

这里是上面,我想获取所有的记录,在我的安卓应用程序中的旋转.But我不明白如何做this.Can有人请帮帮我。在进阶时谢谢。

EN

回答 1

Stack Overflow用户

发布于 2014-08-30 18:56:07

创建一个PHP代码来获取行,并将其以JSON格式从

代码语言:javascript
复制
    $res=mysql_query("select * from table_name where cat_id = '2'") or die(mysql_error());
    $assInfo = array();
    $num=mysql_num_rows($res);
    if($num>0){
        $obj=mysql_fetch_array($res);
        $assInfo["first"]=$obj["column_name"];
        $assInfo["second"]=$obj["another_column"];
        array_push($response["info"], $assInfo);
    }else{
        $response["success"] = 0;
    }
    echo json_encode($response);

在android中,启动一个异步任务在后台抓取打印的文本。

代码语言:javascript
复制
public class gettingData extends AsyncTask<Void, Void, Void> {

private String jsonResult;

public gettingData() {
}

@Override
protected Void doInBackground(Void... arg0) {
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("canpass", "herevalue"));
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
    try {
        httppost.setEntity(new UrlEncodedFormEntity(params));
        HttpResponse response = httpclient.execute(httppost);
        jsonResult = inputStreamToString(response.getEntity().getContent())
                .toString();
        Log.v("jr", jsonResult);
    }

    catch (ClientProtocolException e) {
        Log.e("e", "error1");
        e.printStackTrace();
    } catch (IOException e) {
        Log.e("e", "error2");
        e.printStackTrace();
    }
    return null;
}

@Override
protected void onPostExecute(Void result) {
if (jsonResult != null) {
        try {
            if (jsonResponse.optString("success").matches("0")) {
                 //your code for no rows selected
            }else{
                 JSONArray jsonMainNode = jsonResponse.optJSONArray("info");
                JSONObject jsonChildNode = jsonMainNode.getJSONObject(0);
                String first = jsonChildNode.optString("first");
                String second = jsonChildNode.optString("second");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } else {
        Toast.makeText(context, "Connection Time Out", Toast.LENGTH_LONG)
                .show();
    }
}

在你的活动中执行这个类-

代码语言:javascript
复制
new gettingData().execute();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25581462

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档