Guest User

Untitled

a guest
Nov 16th, 2017
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. private void viewMyprofile(String username) {
  2. System.out.println("in view my profile");
  3. class HttpGetAsyncTask extends AsyncTask<String, Void, String>{
  4. @Override
  5. protected String doInBackground(String... params) {
  6. String lUsername = params[0];
  7.  
  8. HttpClient client = new DefaultHttpClient();
  9. HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
  10. HttpResponse response;
  11. // JSONObject json = new JSONObject();
  12. System.out.println("in do in background");
  13. try{
  14. HttpPost post = new HttpPost(URL);
  15. List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
  16. nameValuePairs.add(new BasicNameValuePair("username", lUsername));
  17. nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi"));
  18. post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  19. response = client.execute(post);
  20. /*Checking response */
  21. if(response!=null){
  22. InputStream in = response.getEntity().getContent(); //Get the data in the entity
  23. jsonResponse = convertStreamToString(in);
  24. System.out.println("this is my response = " + jsonResponse);
  25. return jsonResponse;
  26. }
  27. }
  28. catch(Exception e){
  29. e.printStackTrace();
  30. }
  31. return null ;
  32. }
  33.  
  34. @Override
  35. protected void onPostExecute(String result) {
  36. super.onPostExecute(result);
  37.  
  38. try {
  39. JSONArray jsonArray = new JSONArray(result);
  40. Log.i(MyProfile.class.getName(),
  41. "Number of entries " + jsonArray.length());
  42. for (int i = 0; i < jsonArray.length(); i++) {
  43. JSONObject jsonObject = jsonArray.getJSONObject(i);
  44. Log.i(MyProfile.class.getName(), jsonObject.getString("password"));
  45. textView.setText(jsonObject.getString("password"));
  46. }
  47. } catch (Exception e) {
  48. e.printStackTrace();
  49. }
  50. }
  51. }
  52.  
  53. HttpGetAsyncTask httpGetAsyncTask = new HttpGetAsyncTask();
  54. // Parameter we pass in the execute() method is relate to the first generic type of the AsyncTask
  55. // We are passing the connectWithHttpGet() method arguments to that
  56. httpGetAsyncTask.execute(username);
  57. System.out.println("after execution call");
  58. }
  59.  
  60. public static String convertStreamToString(InputStream is) throws Exception {
  61. BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  62. StringBuilder sb = new StringBuilder();
  63. String line = null;
  64.  
  65. while ((line = reader.readLine()) != null) {
  66. sb.append(line);
  67. }
  68.  
  69. is.close();
  70.  
  71. return sb.toString();
  72. }
  73.  
  74. 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...rnrn Still miss it a lot ... :) ....","memories":"School tym was totally awesome...rnrn Still miss it a lot ... :) ...."}
  75. 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...rnrn 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...rnrn Still miss it a lot ... :) ....","8":""} of type org.json.JSONObject cannot be converted to JSONArray
  76.  
  77. try {
  78. JSONObject jsonObject = new JSONObject(result);
  79. Log.i(MyProfile.class.getName(), jsonObject.getString("password"));
  80. textView.setText(jsonObject.getString("password"));
  81. } catch (Exception e) {
  82. e.printStackTrace();
  83. }
Add Comment
Please, Sign In to add comment