Create user profile activity in java


#1

I want to create a user profile page which should display a particular user information according to their id. below is a code i have already tried.

   mIcon = findViewById(R.id.ivProfile);
    mFollow = findViewById(R.id.btnFollow);

    tvName = findViewById(R.id.tvName);

    // pref = getActivity().getPreferences(0);
     //tvName.setText(SharedPrefManager.getInstance(this).getname());

    final String[] result = {""};
    //the year data to send
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("user", "name"));

    //http post

    final HttpEntity[] entity = {null};
    class RetrieveFeedTask extends AsyncTask<String, Void, Void> {

        private Exception exception;

        protected Void doInBackground(String... urls) {

            try {

                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://192.168.8.100/android_login/getdetails.php");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                entity[0] = response.getEntity();
                InputStream is = entity[0].getContent();

                // } catch (Exception e)
                //   Log.e("log_tag", "Error in http connection " + e.toString());
                //   }
                //InputStream is = entity.getContent();

                //convert response to string
                /*
                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();

                    result[0] = sb.toString();
                } catch (Exception e) {
                    Log.e("log_tag", "Error converting result " + e.toString());
                }
*/
                //parse json data
                try {
                    JSONArray jArray = new JSONArray(result[0]);
                    for (int i = 0; i < jArray.length(); i++) {
                        JSONObject json_data = jArray.getJSONObject(i);
                        Log.i("log_tag", "id: " + json_data.getInt("id") +
                                        ", name: " + json_data.getString("name"));
                                //  ", sex: " + json_data.getInt("sex") +
                                //  ", birthyear: " + json_data.getInt("birthyear")
                        tvName.setText(json_data.getString("name"));


                    }

                } catch (JSONException e) {
                    Log.e("log_tag", "Error parsing data " + e.toString());
                }

                mFollow.setOnClickListener(new View.OnClickListener() {


                    @Override
                    public void onClick(View view) {
                        //String link = "http://192.168.8.100/android_login/add_or_cancel_friend_ship.php";
                        // new updateData();
                        //Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_LONG).show();

                    }
                });


                class updateData {
                    public void main(String[] args) throws IOException {
                        URL url = new URL("http://192.168.8.102/android_login/add_or_cancel_friend_ship.php");
                        HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
                        httpCon.setDoOutput(true);
                        httpCon.setRequestMethod("POST");
                        OutputStreamWriter out = new OutputStreamWriter(
                                httpCon.getOutputStream());
                        System.out.print(httpCon.getResponseCode());
                        System.out.print(httpCon.getResponseMessage());
                        out.close();

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

#2

What is the issue you are facing with your code? Are you getting any errors? Does your code work?


#3

the code don’t just work and it dosen’t display error when i run it