Unnable to send data from android application to PHP webpage (error:Unfortunately application stopped working) -
hello want post string android application website in php using httppost application lay out consists of text box , send button .when click on send button application crashes message unfortunately application stopped working.can body please provide how solve problem.
the mainactivity.java file
package com.example.test5; import java.io.ioexception; import java.io.unsupportedencodingexception; import java.util.arraylist; import java.util.list; 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 com.example.test5.r; import android.os.bundle; import android.app.activity; import android.view.menu; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.toast; public class mainactivity extends activity { button sendbutton; edittext msgtextfield; /** called when activity first created. */ @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // make message text field object msgtextfield = (edittext) findviewbyid(r.id.msgtextfield); // make send button object sendbutton = (button) findviewbyid(r.id.sendbutton); } // function gets called when click button public void send(view v) { // message message text box string msg = msgtextfield.gettext().tostring(); // make sure fields not empty if (msg.length()>0) { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://thawide.com/androidtest.php"); list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(2); namevaluepairs.add(new basicnamevaluepair("id", "12345")); namevaluepairs.add(new basicnamevaluepair("message", msg)); try { httppost.setentity(new urlencodedformentity(namevaluepairs)); } catch (unsupportedencodingexception e) { // todo auto-generated catch block e.printstacktrace(); } try { httpclient.execute(httppost); } catch (clientprotocolexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } msgtextfield.settext(""); // clear text box toast.maketext(getbasecontext(),"sucess",toast.length_short).show(); } else { // display message if text fields empty toast.maketext(getbasecontext(),"all field required",toast.length_short).show(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } }
menifest file
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.test5" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="8" android:targetsdkversion="17" /> <uses-permission android:name="android.permission.internet"> </uses-permission> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name="com.example.test5.mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest>
activity_main.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <edittext android:id="@+id/msgtextfield" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignleft="@+id/sendbutton" android:layout_below="@+id/textview2" android:layout_margintop="59dp" android:ems="10" /> <button android:id="@+id/sendbutton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignleft="@+id/textview1" android:layout_below="@+id/msgtextfield" android:layout_margintop="72dp" android:onclick="send" android:text="send" /> <textview android:id="@+id/textview2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignleft="@+id/msgtextfield" android:layout_below="@+id/textview1" android:layout_margintop="60dp" android:text="message" /> </relativelayout>
server side code
<?php // "message" variable post request // data coming android app $message=$_post["message"]; // specify file save contents of variable message $filename="androidmessages.html"; // write (append) data file //file_put_contents($filename,$message."<br />",file_append); $fh = fopen("androidmessages.html", "wb"); fwrite($fh, $message."<br />"); fclose($fh); // load contents of file variable $androidmessages=file_get_contents($filename); // display contents of variable (which has contents of file) echo $androidmessages; ?>
you should not perform network io on ui thread. you'll need use asynctask
or thread
perform network transaction on background thread.
you can refer tutorial more understanding.
Comments
Post a Comment