Skip to content

Instantly share code, notes, and snippets.

@adaptivedev
Created March 7, 2014 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adaptivedev/9412628 to your computer and use it in GitHub Desktop.
Save adaptivedev/9412628 to your computer and use it in GitHub Desktop.
PostData.java
package com.example.app;
import android.os.AsyncTask;
import android.util.Log;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
/**
* Created by rover on 3/6/14.
*/
public class PostScore extends AsyncTask<String, Integer, String>
{
public String m_name="";
public int m_score=1;
protected String doInBackground(String... args) {
m_name = args[0];
m_score = new Integer(args[1]);
HttpResponse response = null;
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://mysite.com:3000/scores");
//HttpPost httppost = new HttpPost(urls.toString());
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("name", m_name));
nameValuePairs.add(new BasicNameValuePair("score", ""+m_score));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
response = httpclient.execute(httppost);
//Log.d("MainActivity.java", "response="+response);
} catch (ClientProtocolException e) {
Log.d("MainActivity.java", "ClientProtocolException e=" + e);
// TODO Auto-generated catch block
} catch (IOException e) {
Log.d("MainActivity.java", "IOException="+e);
// TODO Auto-generated catch block
}
return response.toString();
}
protected void onProgressUpdate(Integer... progress) {
}
protected void onPostExecute(Long result) {
}
}
03-07 16:34:08.382 3403-3559/com.example.app D/MainActivity.java﹕ IOException=java.net.SocketException: The operation timed out
03-07 16:34:08.382 3403-3559/com.example.app W/dalvikvm﹕ threadid=9: thread exiting with uncaught exception (group=0x4001f888)
03-07 16:34:08.382 3403-3559/com.example.app I/Process﹕ Sending signal. PID: 3403 SIG: 9
03-07 16:34:23.500 3604-3604/com.example.app D/skia﹕ SHAPER: stream created /data/data/com.jungle.app.fonts/tmp/SJjunglego.ttf.. 0x25FDC0
03-07 16:34:23.531 3604-3604/com.example.app D/MainActivity.java﹕ m_username=null
03-07 16:35:04.375 3663-3663/com.example.app D/skia﹕ SHAPER: stream created /data/data/com.jungle.app.fonts/tmp/SJjunglego.ttf.. 0x25FE68
03-07 16:35:04.382 3663-3663/com.example.app D/MainActivity.java﹕ m_username=null
03-07 16:35:05.343 3663-3663/com.example.app D/TextView﹕ onTapUpEvent() : start = 0, end = 0
03-07 16:35:14.195 3663-3663/com.example.app D/dalvikvm﹕ GC_FOR_MALLOC freed 6328 objects / 292240 bytes in 104ms
03-07 16:35:28.976 3663-3675/com.example.app D/MainActivity.java﹕ IOException=java.net.SocketException: The operation timed out
03-07 16:35:28.976 3663-3675/com.example.app W/dalvikvm﹕ threadid=9: thread exiting with uncaught exception (group=0x4001f888)
03-07 16:35:29.023 3663-3675/com.example.app E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:200)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
at java.lang.Thread.run(Thread.java:1096)
Caused by: java.lang.NullPointerException
at com.example.app.PostScore.doInBackground(PostScore.java:56)
at com.example.app.PostScore.doInBackground(PostScore.java:23)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
            at java.lang.Thread.run(Thread.java:1096)
03-07 16:35:30.132 3663-3677/com.example.app D/MainActivity.java﹕ IOException=java.net.SocketException: The operation timed out
03-07 16:35:30.156 3663-3677/com.example.app W/dalvikvm﹕ threadid=10: thread exiting with uncaught exception (group=0x4001f888)
03-07 16:35:30.156 3663-3677/com.example.app I/Process﹕ Sending signal. PI
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment