-
July 30th, 2012, 04:38 PM
#1
App Crashes
Im new to android app and trying to write my first app for learning.
But when i click on Sent To Server button app crashes :-(
here is the code
Code:
package lexcode.online;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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 android.R.string;
import android.app.Activity;
import android.widget.*;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class LexcodeActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) this.findViewById(R.id.submit);
b.setOnClickListener(this);
Button s = (Button) this.findViewById(R.id.send);
s.setOnClickListener(this);
Button ss = (Button) this.findViewById(R.id.sendtoserver);
ss.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch ( v.getId())
{
case R.id.submit:
EditText txtName = (EditText) this.findViewById(R.id.textBox);
TextView txtStatus = (TextView) this.findViewById(R.id.response);
String str = txtName.getText().toString();
txtStatus.setText(str);
break;
case R.id.send:
TextView txtStatus1 = (TextView) this.findViewById(R.id.textBox);
String strRes = "Send Button Clicked ";
txtStatus1.setText(strRes);
break;
case R.id.sendtoserver:
postData();
TextView txtStatus11 = (TextView) this.findViewById(R.id.textBox);
String strRes1 = "Sent to server ";
txtStatus11.setText(strRes1);
}
}
public void postData() {
//Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.mywebsite.com/save.php");
try {
// Add your data
List <NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
//e.printStackTrace();
// TODO Auto-generated catch block
} catch (IOException e) {
//e.printStackTrace();
// TODO Auto-generated catch block
}
}
}
please rate the post if this is useful. And also never forget to mark the thread as [Resolved] when your problem is solved
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|