来自JSONArray的对象的逻辑错误

时间:2014-03-29 09:10:37

标签: android json

我尝试加载主题细节,但它只提供表格(学生)第一行的值,即使我选择了不同的主题。

我检查了上一节带来的id值。身份似乎很好。

请帮帮我。

这是我带来价值的代码。

          // getting id from intent
       user_id= i.getStringExtra(TAG_USER_ID);
    TextView test = (TextView) findViewById(R.id.user_id);
    test.setText(user_id);

这是JSON的代码。

protected JSONObject doInBackground(String... args) {

        JSONObject json = null;     

        try {
                //Building parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("user_id", user_id));

                json = jsonParser.makeHttpRequest(url_subject_details, "GET", params);

                // check your log for json response
                Log.d("Single Subject Details", json.toString());
            }

        catch(Exception e)
            {
              e.printStackTrace();
            }   
        return json;

    }


protected void onPostExecute(JSONObject  json) {
        super.onPostExecute(json);
        // dismiss the dialog once product deleted
        pDialog.dismiss();


        try{
            // json success tag     
            int success;

            success = json.getInt(TAG_SUCCESS);

            // successfully received subject details
            JSONArray studentObj = json.getJSONArray(TAG_STUDENT); // JSON Array

            // get subject object from JSON Array
            JSONObject subject = studentObj.getJSONObject(0);

            if (success == 1) {                                          
                txtName = (EditText) findViewById(R.id.inputName);
                txtEmail = (EditText) findViewById(R.id.inputEmail);

                // display subject data in EditText
                txtName.setText(subject.getString(TAG_NAME));
                txtEmail.setText(subject.getString(TAG_EMAIL)); 
            } else {

            }
        } catch (Exception e) {
              e.printStackTrace();
        }
    }

这是JSONParser.java

public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method,
        List<NameValuePair> params) {

    // Making HTTP request
    try {

        // check for request method
        if(method == "POST"){
            // request method is POST
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        }else if(method == "GET"){
            // request method is GET
            DefaultHttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format(params, "utf-8");
            url += "?" + paramString;
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        }           


    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}
 }

1 个答案:

答案 0 :(得分:0)

您需要从

更改此处
JSONObject subject = studentObj.getJSONObject(0);

for (int i = 0; i < studentObj.length(); i++) {
    JSONObject subject = studentObj.getJSONObject(i);

     name = c.getString(TAG_NAME);
     email = c.getString(TAG_EMAIL);

}

还要全局制作字符串nameemail