Hi have written some code which is supposed to send data via a http request 
object in android, However i know the data is being sent or posted but the 
UI thread does not like the way i have done things can anyone advise please?

Code: 

async class definition: 

private class Connection extends AsyncTask{
    @Override
    protected Object doInBackground(Object[] objects){
        try{
            PostData(R.id.fullscreen_content, 3);
           }
                catch(IOException exception){
                    exception.printStackTrace();
                }
                return null;
            }
}

button click action:  
/** Called when the user clicks the Happy button */
public void happy_click(View view) throws IOException {
    try{
        new Connection().execute();
        report_success();
    }
    catch(Exception exception){
        messageBox("Response was not successful","Failed to process response" + 
exception.getMessage());
    }
}


PostData method: 


protected void PostData(Integer Question_ID,Integer ResponseChosen_ID) throws 
IOException {
    try{
        URL url = new URL("http://cce.swlgroup.com/";);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost post = new HttpPost(conn.getURL().toString());
        conn.setRequestMethod("POST");

        conn.connect();
        Date date = new Date();
        SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd", Locale.UK);
        SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss",Locale.UK);

        String nowDate = dt.format(date);
        String nowTime = time.format(date);

        String output = nowDate + "," + nowTime + ",";

        ArrayList<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("Question 
ID",Question_ID.toString()));
        params.add(new BasicNameValuePair("Response_Chosen_ID", 
ResponseChosen_ID.toString()));
        params.add(new BasicNameValuePair("Timestamp", output));

        post.setEntity(new UrlEncodedFormEntity(params));
        HttpResponse response = httpClient.execute(post);
        BufferedReader reader = new BufferedReader(new 
InputStreamReader(response.getEntity().getContent()));
        StringBuilder builder = new StringBuilder();
        String line = "";
        while((line = reader.readLine()) != null){
            builder.append(params);
        }
        reader.close();
        messageBox("Response Submitted", "Response Submitted to server" + 
builder);
    }
    catch(Exception exception){
        messageBox("Response Failed",exception.getMessage());
    }
}


any advise would be much appreciated. Thanks for your time. 

-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
To post to this group, send email to android-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/4bdf0f0b-bde5-41b1-82a9-81ba647c414a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to