Dileep1994

Untitled

Mar 20th, 2016
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.23 KB | None | 0 0
  1. package com.example.dileep.doubtszone;
  2.  
  3. import android.app.AlertDialog;
  4. import android.app.ProgressDialog;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.net.ConnectivityManager;
  8. import android.net.NetworkInfo;
  9. import android.os.AsyncTask;
  10. import android.support.v7.app.AppCompatActivity;
  11. import android.os.Bundle;
  12. import android.view.View;
  13. import android.widget.Button;
  14. import android.widget.EditText;
  15. import android.widget.ImageButton;
  16. import android.widget.TextView;
  17. import android.widget.Toast;
  18.  
  19. import org.json.JSONArray;
  20. import org.json.JSONException;
  21. import org.json.JSONObject;
  22.  
  23. import java.io.BufferedReader;
  24. import java.io.IOException;
  25. import java.io.InputStreamReader;
  26. import java.io.OutputStreamWriter;
  27. import java.net.MalformedURLException;
  28. import java.net.URL;
  29. import java.net.URLConnection;
  30. import java.net.URLEncoder;
  31.  
  32. public class FinallyAnswerQuery extends AppCompatActivity {
  33.  
  34. TextView tvusername,tvusernamecontent,tvyear,tvyearcontent,tvquestion,tvquestioncontent,tvanswer;
  35. Button btanswer;
  36. EditText etanswer;
  37. UserSessionData userSessionData;
  38. User user;
  39. String json_string="",qid="",repliedby="",qdesc="";
  40. JSONObject jsonObject;
  41. JSONArray jsonArray;
  42.  
  43. @Override
  44. protected void onCreate(Bundle savedInstanceState) {
  45. super.onCreate(savedInstanceState);
  46. setContentView(R.layout.finally_answer_query);
  47. tvusername=(TextView)findViewById(R.id.finally_answer_query_tvUsername);
  48. tvusernamecontent=(TextView)findViewById(R.id.finally_answer_query_tvUsernameContent);
  49. tvyear=(TextView)findViewById(R.id.finally_answer_query_tvYear);
  50. tvyearcontent=(TextView)findViewById(R.id.finally_answer_query_tvYearContent);
  51. tvquestion=(TextView)findViewById(R.id.finally_answer_query_tvQuestion);
  52. tvquestioncontent=(TextView)findViewById(R.id.finally_answer_query_tvQuestionContent);
  53. tvanswer=(TextView)findViewById(R.id.finally_answer_query_tvAnswer);
  54. etanswer=(EditText)findViewById(R.id.finally_answer_query_etAnswer);
  55. btanswer=(Button)findViewById(R.id.finally_answer_query_btAnswer);
  56. ConnectivityManager connectivityManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
  57. NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();
  58. if(!(networkInfo!=null && networkInfo.isConnected())){
  59. Toast.makeText(this, "No internet connection found ", Toast.LENGTH_SHORT).show();
  60. startActivity(new Intent(this, FacultyHome.class));
  61. }
  62. else{
  63. userSessionData=new UserSessionData(this);
  64. user=userSessionData.getUserLoggedIn();
  65. if(user==null){
  66. startActivity(new Intent(this,LoginPage.class));
  67. }
  68. else{
  69. tvusernamecontent.setText(getIntent().getStringExtra("username"));
  70. tvyearcontent.setText(getIntent().getStringExtra("year"));
  71. qdesc=getIntent().getStringExtra("qdesc")+"?";
  72. tvquestioncontent.setText(qdesc);
  73. qid=getIntent().getStringExtra("qid");
  74. repliedby=user.username;
  75. }
  76. }
  77.  
  78.  
  79. }
  80.  
  81. @Override
  82. public void onBackPressed() {
  83. super.onBackPressed();
  84. startActivity(new Intent(this, AnswerQuery.class));
  85. }
  86. public void onAnswerQueryClick(View view){
  87. ConnectivityManager connectivityManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
  88. NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();
  89. if(!(networkInfo!=null && networkInfo.isConnected()))
  90. {
  91. Toast.makeText(this, "No internet connection found ", Toast.LENGTH_SHORT).show();
  92. }
  93. else {
  94. if(etanswer.getText().toString().trim().length()<1){
  95. Toast.makeText(FinallyAnswerQuery.this, "Enter the answer", Toast.LENGTH_SHORT).show();
  96. }
  97. else{
  98. AnswerQueryTask answerQueryTask=new AnswerQueryTask();
  99. answerQueryTask.execute(qid,etanswer.getText().toString().trim(),repliedby);
  100. }
  101.  
  102.  
  103. }
  104. }
  105.  
  106. class AnswerQueryTask extends AsyncTask<String,Void,String> {
  107.  
  108. String login_url="";
  109.  
  110. private ProgressDialog pd;
  111. @Override
  112. protected void onPreExecute() {
  113. login_url="http://jntuknews.com/Answer_Query.php";
  114. pd = new ProgressDialog(FinallyAnswerQuery.this);
  115. pd.setTitle("Submitting answer ...");
  116. pd.setMessage("Please wait...");
  117. pd.setCancelable(false);
  118. pd.setIndeterminate(true);
  119. pd.show();
  120. }
  121.  
  122. @Override
  123. protected String doInBackground(String... params) {
  124. String id=params[0],ans=params[1],rby=params[2];
  125. try {
  126. URL url=new URL(login_url);
  127. URLConnection urlConnection=url.openConnection();
  128. urlConnection.setDoOutput(true);
  129. OutputStreamWriter outputStreamWriter=new OutputStreamWriter(urlConnection.getOutputStream());
  130. String data = URLEncoder.encode("id", "UTF-8")+"="+URLEncoder.encode(id,"UTF-8")+"&"+
  131. URLEncoder.encode("answer", "UTF-8")+"="+URLEncoder.encode(ans,"UTF-8")+"&"+
  132. URLEncoder.encode("replied_by", "UTF-8")+"="+URLEncoder.encode(rby,"UTF-8");
  133. outputStreamWriter.write(data);
  134. outputStreamWriter.flush();
  135. BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
  136. StringBuilder stringBuilder=new StringBuilder();
  137. while ((json_string=bufferedReader.readLine())!=null)
  138. {
  139. stringBuilder.append(json_string+"\n");
  140. }
  141. outputStreamWriter.close();
  142. bufferedReader.close();
  143. return stringBuilder.toString().trim();
  144. } catch (MalformedURLException e) {
  145. e.printStackTrace();
  146. } catch (IOException e) {
  147. e.printStackTrace();
  148. }
  149.  
  150. return null;
  151. }
  152.  
  153. @Override
  154. protected void onProgressUpdate(Void... values) {
  155. super.onProgressUpdate(values);
  156. }
  157.  
  158. @Override
  159. protected void onPostExecute(String result) {
  160. try {
  161. jsonObject=new JSONObject(result);
  162. jsonArray=jsonObject.getJSONArray("server_response");
  163. int count=0;
  164. String message="";
  165. while(count<jsonArray.length()) {
  166. JSONObject jsonObject1 = jsonArray.getJSONObject(count);
  167. message = jsonObject1.getString("message");
  168. count++;
  169. }
  170. AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(FinallyAnswerQuery.this);
  171. switch (message){
  172. case "1":dialogBuilder.setMessage("Answered query Successfully");
  173. dialogBuilder.setPositiveButton("Ok", null);
  174. dialogBuilder.show();
  175. startActivity(new Intent(FinallyAnswerQuery.this, AnswerQuery.class));
  176. break;
  177. case "2":dialogBuilder.setMessage("Error in answering the question");
  178. dialogBuilder.setPositiveButton("Ok", null);
  179. dialogBuilder.show();
  180. break;
  181. case "3":dialogBuilder.setMessage("Query already answered");
  182. dialogBuilder.setPositiveButton("Ok", null);
  183. dialogBuilder.show();
  184. startActivity(new Intent(FinallyAnswerQuery.this,AnswerQuery.class));
  185. break;
  186. }
  187.  
  188.  
  189. } catch (JSONException e) {
  190. Toast.makeText(FinallyAnswerQuery.this, "Error in json parsing", Toast.LENGTH_SHORT).show();
  191. e.printStackTrace();
  192. }
  193. if (pd!=null) {
  194. pd.dismiss();
  195. }
  196. }
  197. }
  198. }
Add Comment
Please, Sign In to add comment