Skip to content

Instantly share code, notes, and snippets.

@danpariente
Created October 8, 2013 01:59
Show Gist options
  • Save danpariente/6878223 to your computer and use it in GitHub Desktop.
Save danpariente/6878223 to your computer and use it in GitHub Desktop.
package com.apptegy.arkadelphia.questions;
import android.app.ProgressDialog;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.apptegy.arkadelphia.BaseActivity;
import com.apptegy.arkadelphia.R;
import com.apptegy.arkadelphia.live.LiveFeed;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
/**
* Created by Rafael on 09-29-13.
*/
public class AskQuestion extends BaseActivity {
TextView lblQuestion,lblResponse,txtIam;
Spinner sprDept,sprIam;
TextView txtPhone,txtEmail,txtQuestion;
Button btnSubmit;
String msg;
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ask_question);
super.showActionButton(false);
super.setActionBarTitle("Ask a question");
String[] depts = {"Administration","Athletics","PTO"};
String[] iam = {"Student","Parent"};
lblQuestion = (TextView)findViewById(R.id.lblQuestion);
lblResponse = (TextView)findViewById(R.id.lblResponse);
txtIam = (TextView)findViewById(R.id.txtIam);
Typeface tf = Typeface.createFromAsset(this.getAssets(),"fonts/HelveticaNeueLTStd-Lt.otf");
lblQuestion.setTypeface(tf);
lblResponse.setTypeface(tf);
txtIam.setTypeface(tf);
sprDept = (Spinner)findViewById(R.id.sprQuestionTo);
ArrayAdapter<String> deptArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,depts);
sprDept.setAdapter(deptArrayAdapter);
sprIam = (Spinner)findViewById(R.id.sprIam);
ArrayAdapter<String> iamArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,iam);
sprIam.setAdapter(iamArrayAdapter);
txtPhone = (TextView)findViewById(R.id.txtPhone);
txtEmail = (TextView)findViewById(R.id.txtEmail);
txtQuestion = (TextView)findViewById(R.id.txtQuestion);
btnSubmit = (Button)findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new PostAsyncTask().execute();
}
});
}
class PostAsyncTask extends AsyncTask<String, Integer, Integer> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(AskQuestion.this);
progressDialog.setCancelable(true);
progressDialog.setMessage("Sending Question...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setProgress(0);
progressDialog.show();
}
@Override
protected Integer doInBackground(String... arg0) {
int r=0;
String post_url="http://arkadelphiaar.apptegy.net/api/v1/s/ask-question--4/section_types/1/sections.json?token=VqbKv2brhiJqPa2dthYx";
String start = "{\"section\":{\"properties\":{";
String end = "}}}";
String json =start+ "\"question\":"+"\""+txtQuestion.getText()+"\""+
",\"from\":\""+sprIam.getSelectedItem().toString()+"\""+
",\"department\":\""+sprDept.getSelectedItem().toString()+"\""+
",\"email\":\""+txtEmail.getText()+"\""+
",\"phone_number\":\""+txtPhone.getText()+"\""+end;
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
HttpResponse response;
try {
HttpPost post = new HttpPost(post_url);
StringEntity se = new StringEntity(json);
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
response = client.execute(post);
if(response!=null){
InputStream in = response.getEntity().getContent(); //Get the data in the entity
r=1;
}
} catch(Exception e) {
msg=e.getMessage();
r=-1;
}
return r;
}
@Override
protected void onPostExecute(Integer result) {
progressDialog.dismiss();
if (result==1)
{
Toast.makeText(getApplicationContext(),"Question posted successfully",Toast.LENGTH_LONG).show();
}
if (result==-1)
{
Toast.makeText(getApplicationContext(),"Error: "+msg,Toast.LENGTH_LONG).show();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment