将字符串响应转换为json数组时出错 - android

时间:2013-01-02 03:32:19

标签: android json string response

我使用ASYNC任务使用HTTPPost与互联网进行交互... 这是java代码

private void viewMyprofile(String username) {
    System.out.println("in view my profile");
    class HttpGetAsyncTask extends AsyncTask<String, Void, String>{
        @Override
        protected String doInBackground(String... params) {
            String lUsername = params[0];

            HttpClient client = new DefaultHttpClient();
            HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
            HttpResponse response;
           // JSONObject json = new JSONObject();
            System.out.println("in do in background");
            try{
                HttpPost post = new HttpPost(URL);
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("username", lUsername));
                nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi"));
                post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                response = client.execute(post);
                /*Checking response */
                if(response!=null){
                    InputStream in = response.getEntity().getContent(); //Get the data in the entity 
                    jsonResponse = convertStreamToString(in);
                    System.out.println("this is my response = " + jsonResponse);
                    return jsonResponse;
                }
            }
            catch(Exception e){
                e.printStackTrace();
            }
            return null ;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            try {
                JSONArray jsonArray = new JSONArray(result);
                Log.i(MyProfile.class.getName(),
                    "Number of entries " + jsonArray.length());
                for (int i = 0; i < jsonArray.length(); i++) {
                  JSONObject jsonObject = jsonArray.getJSONObject(i);
                  Log.i(MyProfile.class.getName(), jsonObject.getString("password"));
                  textView.setText(jsonObject.getString("password"));
                }
              } catch (Exception e) {
                e.printStackTrace();
              }
        }
    }

    HttpGetAsyncTask httpGetAsyncTask = new HttpGetAsyncTask();
    // Parameter we pass in the execute() method is relate to the first generic type of the AsyncTask
    // We are passing the connectWithHttpGet() method arguments to that
    httpGetAsyncTask.execute(username);
    System.out.println("after execution call");
}

public static String convertStreamToString(InputStream is) throws Exception {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line = null;

    while ((line = reader.readLine()) != null) {
        sb.append(line);
    }

    is.close();

    return sb.toString();
}

}

这是两条logcat线......

01-02 08:51:31.789: I/System.out(21879): this is my response =   {"0":"1","id":"1","1":"205041316950ae6ca20e6ac8.73557365","userid":"205041316950ae6ca20e6ac8.73557365","2":"Moderators","name":"Moderators","3":"ADMIN","username":"ADMIN","4":"umairisback","password":"umairisback","5":"omerjerk@gmail.com","email":"omerjerk@gmail.com","6":"1234567890","phone":"1234567890","7":"2012","batch":"2012","8":"","stream":"","9":"15\/08\/1994","dob":"15\/08\/1994","10":"on the PC!!!    \\m\/","address":"on the PC!!!    \\m\/","11":"Delhi Technological University","college":"Delhi Technological University","12":"Web Developer at LudlowCastle.co.in","occupation":"Web Developer at LudlowCastle.co.in","13":"School tym was totally awesome...\r\n\r\n Still miss it a lot ... :) ....","memories":"School tym was totally awesome...\r\n\r\n Still miss it a lot ... :) ...."}
01-02 08:51:31.809: W/System.err(21879): org.json.JSONException: Value {"stream":"","phone":"1234567890","college":"Delhi Technological University","userid":"205041316950ae6ca20e6ac8.73557365","13":"School tym was totally awesome...\r\n\r\n Still miss it a lot ... :) ....","password":"umairisback","11":"Delhi Technological University","12":"Web Developer at LudlowCastle.co.in","id":"1","username":"ADMIN","name":"Moderators","occupation":"Web Developer at LudlowCastle.co.in","3":"ADMIN","2":"Moderators","10":"on the PC!!!    \\m\/","1":"205041316950ae6ca20e6ac8.73557365","0":"1","7":"2012","address":"on the PC!!!    \\m\/","6":"1234567890","email":"omerjerk@gmail.com","batch":"2012","5":"omerjerk@gmail.com","dob":"15\/08\/1994","4":"umairisback","9":"15\/08\/1994","memories":"School tym was totally awesome...\r\n\r\n Still miss it a lot ... :) ....","8":""} of type org.json.JSONObject cannot be converted to JSONArray

你可以看到我在将响应字符串转换为json数组时遇到错误... 请帮帮我......

P.S。我是新手......

1 个答案:

答案 0 :(得分:1)

根据你的输出日志,你没有得到一个数组,你得到一个JSONObject。所以只是......

try {
     JSONObject jsonObject = new JSONObject(result);
     Log.i(MyProfile.class.getName(), jsonObject.getString("password"));
     textView.setText(jsonObject.getString("password"));
} catch (Exception e) {
     e.printStackTrace();
}

应记录并将textView的文本设置为umairisback