Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.dileep.doubtszone;
- import android.app.AlertDialog;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.Intent;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- import android.os.AsyncTask;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ImageButton;
- import android.widget.TextView;
- import android.widget.Toast;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLConnection;
- import java.net.URLEncoder;
- public class FinallyAnswerQuery extends AppCompatActivity {
- TextView tvusername,tvusernamecontent,tvyear,tvyearcontent,tvquestion,tvquestioncontent,tvanswer;
- Button btanswer;
- EditText etanswer;
- UserSessionData userSessionData;
- User user;
- String json_string="",qid="",repliedby="",qdesc="";
- JSONObject jsonObject;
- JSONArray jsonArray;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.finally_answer_query);
- tvusername=(TextView)findViewById(R.id.finally_answer_query_tvUsername);
- tvusernamecontent=(TextView)findViewById(R.id.finally_answer_query_tvUsernameContent);
- tvyear=(TextView)findViewById(R.id.finally_answer_query_tvYear);
- tvyearcontent=(TextView)findViewById(R.id.finally_answer_query_tvYearContent);
- tvquestion=(TextView)findViewById(R.id.finally_answer_query_tvQuestion);
- tvquestioncontent=(TextView)findViewById(R.id.finally_answer_query_tvQuestionContent);
- tvanswer=(TextView)findViewById(R.id.finally_answer_query_tvAnswer);
- etanswer=(EditText)findViewById(R.id.finally_answer_query_etAnswer);
- btanswer=(Button)findViewById(R.id.finally_answer_query_btAnswer);
- ConnectivityManager connectivityManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();
- if(!(networkInfo!=null && networkInfo.isConnected())){
- Toast.makeText(this, "No internet connection found ", Toast.LENGTH_SHORT).show();
- startActivity(new Intent(this, FacultyHome.class));
- }
- else{
- userSessionData=new UserSessionData(this);
- user=userSessionData.getUserLoggedIn();
- if(user==null){
- startActivity(new Intent(this,LoginPage.class));
- }
- else{
- tvusernamecontent.setText(getIntent().getStringExtra("username"));
- tvyearcontent.setText(getIntent().getStringExtra("year"));
- qdesc=getIntent().getStringExtra("qdesc")+"?";
- tvquestioncontent.setText(qdesc);
- qid=getIntent().getStringExtra("qid");
- repliedby=user.username;
- }
- }
- }
- @Override
- public void onBackPressed() {
- super.onBackPressed();
- startActivity(new Intent(this, AnswerQuery.class));
- }
- public void onAnswerQueryClick(View view){
- ConnectivityManager connectivityManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();
- if(!(networkInfo!=null && networkInfo.isConnected()))
- {
- Toast.makeText(this, "No internet connection found ", Toast.LENGTH_SHORT).show();
- }
- else {
- if(etanswer.getText().toString().trim().length()<1){
- Toast.makeText(FinallyAnswerQuery.this, "Enter the answer", Toast.LENGTH_SHORT).show();
- }
- else{
- AnswerQueryTask answerQueryTask=new AnswerQueryTask();
- answerQueryTask.execute(qid,etanswer.getText().toString().trim(),repliedby);
- }
- }
- }
- class AnswerQueryTask extends AsyncTask<String,Void,String> {
- String login_url="";
- private ProgressDialog pd;
- @Override
- protected void onPreExecute() {
- login_url="http://jntuknews.com/Answer_Query.php";
- pd = new ProgressDialog(FinallyAnswerQuery.this);
- pd.setTitle("Submitting answer ...");
- pd.setMessage("Please wait...");
- pd.setCancelable(false);
- pd.setIndeterminate(true);
- pd.show();
- }
- @Override
- protected String doInBackground(String... params) {
- String id=params[0],ans=params[1],rby=params[2];
- try {
- URL url=new URL(login_url);
- URLConnection urlConnection=url.openConnection();
- urlConnection.setDoOutput(true);
- OutputStreamWriter outputStreamWriter=new OutputStreamWriter(urlConnection.getOutputStream());
- String data = URLEncoder.encode("id", "UTF-8")+"="+URLEncoder.encode(id,"UTF-8")+"&"+
- URLEncoder.encode("answer", "UTF-8")+"="+URLEncoder.encode(ans,"UTF-8")+"&"+
- URLEncoder.encode("replied_by", "UTF-8")+"="+URLEncoder.encode(rby,"UTF-8");
- outputStreamWriter.write(data);
- outputStreamWriter.flush();
- BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
- StringBuilder stringBuilder=new StringBuilder();
- while ((json_string=bufferedReader.readLine())!=null)
- {
- stringBuilder.append(json_string+"\n");
- }
- outputStreamWriter.close();
- bufferedReader.close();
- return stringBuilder.toString().trim();
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
- @Override
- protected void onProgressUpdate(Void... values) {
- super.onProgressUpdate(values);
- }
- @Override
- protected void onPostExecute(String result) {
- try {
- jsonObject=new JSONObject(result);
- jsonArray=jsonObject.getJSONArray("server_response");
- int count=0;
- String message="";
- while(count<jsonArray.length()) {
- JSONObject jsonObject1 = jsonArray.getJSONObject(count);
- message = jsonObject1.getString("message");
- count++;
- }
- AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(FinallyAnswerQuery.this);
- switch (message){
- case "1":dialogBuilder.setMessage("Answered query Successfully");
- dialogBuilder.setPositiveButton("Ok", null);
- dialogBuilder.show();
- startActivity(new Intent(FinallyAnswerQuery.this, AnswerQuery.class));
- break;
- case "2":dialogBuilder.setMessage("Error in answering the question");
- dialogBuilder.setPositiveButton("Ok", null);
- dialogBuilder.show();
- break;
- case "3":dialogBuilder.setMessage("Query already answered");
- dialogBuilder.setPositiveButton("Ok", null);
- dialogBuilder.show();
- startActivity(new Intent(FinallyAnswerQuery.this,AnswerQuery.class));
- break;
- }
- } catch (JSONException e) {
- Toast.makeText(FinallyAnswerQuery.this, "Error in json parsing", Toast.LENGTH_SHORT).show();
- e.printStackTrace();
- }
- if (pd!=null) {
- pd.dismiss();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment