I have been searching for a way to send data to a php page on a remote server and cant seem to get it to work...I modified the code from a similar question on here but it doesnt work still..do I have to put the httppost in an asynctask or something? How would I do that? Code:
public class gameOver extends Activity {
//TextView hiscores;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gameover);
final Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Perform action on click
Toast.makeText(gameOver.this, "button was pressed",
Toast.LENGTH_SHORT).show();
try {
JSONObject json = new JSONObject();
json.put("timestamp", 1351181576.64078);
json.put("name", "engine_speed");
json.put("value", 714.0);
postData(json);
JSONObject json2 = new JSONObject();
json2.put("timestamp", 1351181576.7207818);
json2.put("name", "steering_wheel_angle");
json2.put("value", 11.1633);
postData(json2);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
public void postData(JSONObject json) throws JSONException {
HttpClient httpclient = new DefaultHttpClient();
String URL = "http://www.mysite.com/s.php";
try {
HttpPost httppost = new HttpPost(URL);
List<NameValuePair> nvp = new ArrayList<NameValuePair>(2);
nvp.add(new BasicNameValuePair("json", json.toString()));
httppost.setEntity(new UrlEncodedFormEntity(nvp));
HttpResponse response = httpclient.execute(httppost);
if(response != null) {
InputStream is = response.getEntity().getContent();
//input stream is response that can be shown back on android
Toast.makeText(gameOver.this, (CharSequence) is,
Toast.LENGTH_SHORT).show();
}else{ Toast.makeText(gameOver.this, "data Not sent",
Toast.LENGTH_SHORT).show();}
}catch (Exception e) {
e.printStackTrace();
}
}
}
logcat:
09-06 11:45:28.986: W/System.err(30433): android.os.NetworkOnMainThreadException
09-06 11:45:28.986: W/System.err(30433): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
09-06 11:45:28.986: W/System.err(30433): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
09-06 11:45:28.986: W/System.err(30433): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
09-06 11:45:28.986: W/System.err(30433): at java.net.InetAddress.getAllByName(InetAddress.java:214)
09-06 11:45:28.986: W/System.err(30433): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
09-06 11:45:28.986: W/System.err(30433): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
09-06 11:45:28.986: W/System.err(30433): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
09-06 11:45:28.986: W/System.err(30433): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
09-06 11:45:28.986: W/System.err(30433): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
09-06 11:45:28.986: W/System.err(30433): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
09-06 11:45:28.986: W/System.err(30433): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
09-06 11:45:28.986: W/System.err(30433): at com.example.testgallery.gameOver.postData(gameOver.java:90)
09-06 11:45:28.986: W/System.err(30433): at com.example.testgallery.gameOver$1.onClick(gameOver.java:65)
09-06 11:45:28.986: W/System.err(30433): at android.view.View.performClick(View.java:4240)
09-06 11:45:28.986: W/System.err(30433): at android.view.View$PerformClick.run(View.java:17721)
09-06 11:45:28.986: W/System.err(30433): at android.os.Handler.handleCallback(Handler.java:730)
09-06 11:45:28.986: W/System.err(30433): at android.os.Handler.dispatchMessage(Handler.java:92)
09-06 11:45:28.986: W/System.err(30433): at android.os.Looper.loop(Looper.java:137)
09-06 11:45:28.986: W/System.err(30433): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-06 11:45:28.986: W/System.err(30433): at java.lang.reflect.Method.invokeNative(Native Method)
09-06 11:45:28.986: W/System.err(30433): at java.lang.reflect.Method.invoke(Method.java:525)
09-06 11:45:28.986: W/System.err(30433): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-06 11:45:28.996: W/System.err(30433): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-06 11:45:28.996: W/System.err(30433): at dalvik.system.NativeStart.main(Native Method)
09-06 11:45:28.996: W/System.err(30433): android.os.NetworkOnMainThreadException
09-06 11:45:28.996: W/System.err(30433): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
09-06 11:45:28.996: W/System.err(30433): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
09-06 11:45:28.996: W/System.err(30433): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
09-06 11:45:28.996: W/System.err(30433): at java.net.InetAddress.getAllByName(InetAddress.java:214)
09-06 11:45:28.996: W/System.err(30433): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
09-06 11:45:28.996: W/System.err(30433): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
09-06 11:45:28.996: W/System.err(30433): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
09-06 11:45:28.996: W/System.err(30433): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
09-06 11:45:28.996: W/System.err(30433): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
09-06 11:45:28.996: W/System.err(30433): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
09-06 11:45:28.996: W/System.err(30433): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
09-06 11:45:28.996: W/System.err(30433): at com.example.testgallery.gameOver.postData(gameOver.java:90)
09-06 11:45:28.996: W/System.err(30433): at com.example.testgallery.gameOver$1.onClick(gameOver.java:71)
09-06 11:45:28.996: W/System.err(30433): at android.view.View.performClick(View.java:4240)
09-06 11:45:28.996: W/System.err(30433): at android.view.View$PerformClick.run(View.java:17721)
09-06 11:45:28.996: W/System.err(30433): at android.os.Handler.handleCallback(Handler.java:730)
09-06 11:45:28.996: W/System.err(30433): at android.os.Handler.dispatchMessage(Handler.java:92)
09-06 11:45:28.996: W/System.err(30433): at android.os.Looper.loop(Looper.java:137)
09-06 11:45:28.996: W/System.err(30433): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-06 11:45:28.996: W/System.err(30433): at java.lang.reflect.Method.invokeNative(Native Method)
09-06 11:45:28.996: W/System.err(30433): at java.lang.reflect.Method.invoke(Method.java:525)
09-06 11:45:28.996: W/System.err(30433): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-06 11:45:28.996: W/System.err(30433): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-06 11:45:28.996: W/System.err(30433): at dalvik.system.NativeStart.main(Native Method)
Yes, you should definitely use
AsyncTask
to do that! In your code, you can create a class that extendsAsyncTask
:In this class, you will override 2 methods :
doInBackground
, that will do basically the code you have in youronClick()
method, andonPostExecute()
that wil do some processing with the response of the server.Then in your
onClick
method, you can create an instance of this class and callexecute
on it, passing the url you want to access :EDIT
I remember writing similar code for another person some time ago, you can check this post for another example : getting value using JSON nullpointer
You should you use AsyncTask to make send the data. I do not know your php page however I will try to explain how to send data to a server from android using JSON (If you have any further problems refer to this tutorial http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/). You are sending three values to the server as a POST request. I think your php file should receive the three variables and insert them into a database and then send back data accordingly.
Your php file (I am assuming you want to insert these values into a database named Details having three columns timestamp,name,value and then tell the app user if the data has been inserted or not.)
insert.php
Your class gameOver must have a subclass whiich must extend AsyncTask and do the JSON insertion.
gameOver Activity
The JSONParser class mentioned above is