hi i have developed a app to post data from android to website. now when i run app i got message Unfortunately app has stopped an then ap closed. what is the issue.??
if you have some better idea or tutorial for this purpose please share that also
java code
package com.latlongapp;
import java.io.IOException;
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 android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;
public class MainActivity extends Activity {
Button sendButton;
EditText msgTextField;
@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);
}
public void send(View v)
{
// get the message from the message text box
String msg = msgTextField.getText().toString();
// make sure the fields are not empty
if (msg.length()>0)
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://http://tayyab001.base.pk/kami.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("message", msg));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
msgTextField.setText(""); // clear text box
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
else
{
// display message if text fields are empty
Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show();
}
}
}
XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:text="Message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/msgTextField"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:text="Send"
android:id="@+id/sendButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="send"
/>
</LinearLayout>
manifest
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.latlongapp.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>
LOGCAT
07-15 08:14:25.184: D/gralloc_goldfish(1108): Emulator without GPU emulation detected.
07-15 08:14:38.134: D/dalvikvm(1108): GC_FOR_ALLOC freed 162K, 8% free 3119K/3360K, paused 40ms, total 42ms
07-15 08:14:38.274: D/AndroidRuntime(1108): Shutting down VM
07-15 08:14:38.274: W/dalvikvm(1108): threadid=1: thread exiting with uncaught exception (group=0xb3a5ab90)
07-15 08:14:38.304: E/AndroidRuntime(1108): FATAL EXCEPTION: main
07-15 08:14:38.304: E/AndroidRuntime(1108): Process: com.latlongapp, PID: 1108
07-15 08:14:38.304: E/AndroidRuntime(1108): java.lang.IllegalStateException: Could not execute method of the activity
07-15 08:14:38.304: E/AndroidRuntime(1108): at android.view.View$1.onClick(View.java:3814)
07-15 08:14:38.304: E/AndroidRuntime(1108): at android.view.View.performClick(View.java:4424)
07-15 08:14:38.304: E/AndroidRuntime(1108): at android.view.View$PerformClick.run(View.java:18383)
07-15 08:14:38.304: E/AndroidRuntime(1108): at android.os.Handler.handleCallback(Handler.java:733)
07-15 08:14:38.304: E/AndroidRuntime(1108): at android.os.Handler.dispatchMessage(Handler.java:95)
07-15 08:14:38.304: E/AndroidRuntime(1108): at android.os.Looper.loop(Looper.java:137)
07-15 08:14:38.304: E/AndroidRuntime(1108): at android.app.ActivityThread.main(ActivityThread.java:4998)
07-15 08:14:38.304: E/AndroidRuntime(1108): at java.lang.reflect.Method.invokeNative(Native Method)
07-15 08:14:38.304: E/AndroidRuntime(1108): at java.lang.reflect.Method.invoke(Method.java:515)
07-15 08:14:38.304: E/AndroidRuntime(1108): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
07-15 08:14:38.304: E/AndroidRuntime(1108): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
07-15 08:14:38.304: E/AndroidRuntime(1108): at dalvik.system.NativeStart.main(Native Method)
07-15 08:14:38.304: E/AndroidRuntime(1108): Caused by: java.lang.reflect.InvocationTargetException
07-15 08:14:38.304: E/AndroidRuntime(1108): at java.lang.reflect.Method.invokeNative(Native Method)
07-15 08:14:38.304: E/AndroidRuntime(1108): at java.lang.reflect.Method.invoke(Method.java:515)
07-15 08:14:38.304: E/AndroidRuntime(1108): at android.view.View$1.onClick(View.java:3809)
07-15 08:14:38.304: E/AndroidRuntime(1108): ... 11 more
07-15 08:14:38.304: E/AndroidRuntime(1108): Caused by: android.os.NetworkOnMainThreadException
07-15 08:14:38.304: E/AndroidRuntime(1108): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
07-15 08:14:38.304: E/AndroidRuntime(1108): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
07-15 08:14:38.304: E/AndroidRuntime(1108): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
07-15 08:14:38.304: E/AndroidRuntime(1108): at java.net.InetAddress.getAllByName(InetAddress.java:214)
07-15 08:14:38.304: E/AndroidRuntime(1108): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
07-15 08:14:38.304: E/AndroidRuntime(1108): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-15 08:14:38.304: E/AndroidRuntime(1108): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-15 08:14:38.304: E/AndroidRuntime(1108): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
07-15 08:14:38.304: E/AndroidRuntime(1108): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-15 08:14:38.304: E/AndroidRuntime(1108): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-15 08:14:38.304: E/AndroidRuntime(1108): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-15 08:14:38.304: E/AndroidRuntime(1108): at com.latlongapp.MainActivity.send(MainActivity.java:54)
07-15 08:14:38.304: E/AndroidRuntime(1108): ... 14 more
07-15 08:14:43.674: I/Process(1108): Sending signal. PID: 1108 SIG: 9
PHP SCript being used is
<?php
// get the "message" variable from the post request
// this is the data coming from the Android app
$message=$_POST["message"];
// specify the file where we will save the contents of the variable message
$filename="androidmessages.html";
// write (append) the data to the file
file_put_contents($filename,$message."<br />",FILE_APPEND);
// load the contents of the file to a variable
$androidmessages=file_get_contents($filename);
// display the contents of the variable (which has the contents of the file)
echo $androidmessages;
?>