Reputation: 417
I am new to develop android applications. I need to store ID in in WAMP server. When I try to run my code, emulator shows "Unfortunately myapp has stopped" message and I can't send data from android to PHP.
I am trying to fix this for past two days. I added my activity to the manifest file. Here is my .java file:
public class MainActivity extends Activity {
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
EditText inputid;
private static String url_sample = "http://localhost/android_connect/sample.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Edit Text
inputid = (EditText) findViewById(R.id.editText1);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// creating new product in background thread
new add().execute();
}
});
}
class add extends AsyncTask<String, String,String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("your Registration is processing..wait for few sec..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... args) {
String id = inputid.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id",id));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_sample, "POST", params);
// check log cat for response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
Intent i = getIntent();
setResult(100,i);
// closing this screen
finish();
} else {
// failed to create product
}
}
catch (Exception e) {
e.printStackTrace();
}
return doInBackground();
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
}
PHP code is:
<?php
$response = array();
if (isset($_POST['id']))
{
$userid = $_POST['id'];
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
$result = mysql_query("INSERT INTO id(ID) VALUES('$userid')");
echo $userid;
if ($result)
{
$response["success"] = 1;
$response["message"] = " Registered successfully";
echo json_encode($response);
}
else
{
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
echo json_encode($response);
}
}
else
{
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
echo json_encode($response);
}
?>
no error in both PHP and Android coding..logcat shows error messages..some are
04-09 17:06:02.552: I/Choreographer(10719): Skipped 40 frames! The application may be doing too much work on its main thread.
Upvotes: 3
Views: 484
Reputation: 42040
You seem to be calling your code recursively, in doInBackground(...)
you are once again calling doInBackground
, and also without arguments (probably gives you a NoSuchMethodException
), according to your specification you must return a string, but seeing as you are not using the result you might as well return a null (or change the specification to Void
).
Also, the reason that you are not seeing the stacktrace is that you are probably filtering for log
statements, whilst e.printStackTrace()
does not use log statements.
Edit:
Please use
Log.e("MyActivityNameHere", e.toString())
instead of e.prinStackTrace()
to see the exception
Upvotes: 2
Reputation: 603
If you are using localhost, then you should call the url like this
private static String url_sample = "http://10.0.2.2/android_connect/sample.php";
Upvotes: 0
Reputation: 603
You are getting the error message because of the line
return doInBackground();
you are calling the doInBackground() method recursively, which gives the thread heavy work load.
try to return some string (in your case, just return null;)
Upvotes: 0