如何查看JSON数组以编辑View

时间:2012-01-16 09:18:57

标签: android json

我正在开发android。 我很困惑。因为我不知道将JSON转换为在Edit View中查看(例如)。 很多总统只是在Log。

这是我的JSON文件

EditText txt1 = (EditText) findViewById(R.id.Text1);



    RestClient client = new RestClient("http://192.168.2.79/restserver/index.php/api/example/users"); 
   //client.AddParam("id", "1"); //parameter
    client.AddParam("format", "json"); //parameter format 

    client.AddHeader("GData-Version", "2"); //header
    try { 
        client.Execute(RequestMethod.GET);
    } catch (Exception e) { 
        e.printStackTrace(); 
    } 

    String response = client.getResponse(); 
    Log.i("respon",response);
    //Toast.makeText(this, "json : "+ response, 1).show();

    //create json creation
    try {
        JSONObject json = new JSONObject(response);

         Log.i("respon","<jsonobject>\n"+json.toString()+"\n</json>");
            //Log.i("respon","<jsonobject>\n"+json.t

            JSONArray nameArray = json.names();
            JSONArray valArray = json.toJSONArray(nameArray);

          //try 1
            JSONObject object = (JSONObject) new JSONTokener(response).nextValue();

            String[] id = new String[valArray.length()]; 
            String[] name = new String[valArray.length()]; 

            String Id,Name;
            for(int i=0;i<valArray.length();i++) {

                Log.i("respon","<jsonname"+i+">\n"+nameArray.getString(i)+"\n</jsonname"+i+">\n"
                        +"<jsonvalue"+i+"\n"+valArray.getString(i)+"\n</jsonvalue"+i+">");


                //coba


                JSONObject obj = valArray.getJSONObject(i);
                id[i] = obj.getString("id");
                name[i] = obj.getString("name");


                            //wanna show at EditView, ect (but I can't)

                Toast.makeText(this, "id : " +  " ,name : ", 1).show();
                txt1.setText("id : " + id + " ,name : "+name);


            }

如何解决json不仅可以在日志中查看。但也在对象(小部件android)。 thanx很多人......

1 个答案:

答案 0 :(得分:0)

我认为您无法从JSON获取数据,请使用以下代码。

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("LINK");
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
StatusLine statusLine = response.getStatusLine();
int statuscode = statusLine.getStatusCode();

BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(is, "UTF8"), 16);
stringBuilder = new StringBuilder();
stringBuilder.append(bufferedReader.readLine() + "\n");
String line = "0";

while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line + "\n");


is.close();
Strign result = stringBuilder.toString();

JSONArray jArrayTemp = new JSONArray(result);
JSONObject json_data = null;

userID[i] = json_data.getString("UserID");
userName[i] = json_data.getString("UserName");
userLoginID[i] = json_data.getString("UserLoginID");
userPassword[i] = json_data.getString("UserPassword");

试试上面的代码。

如果可以借助以下链接::

http://www.vogella.de/articles/AndroidJSON/article.html