I am having trouble sending data from my app to a PHP script on my localhost server. I can get information from the PHP script but cannot seem to post information to it. I am trying to send an ID to the file for use in querying a database. When I load the PHP file I just get a blank page after sending the data form the app, which is running on an emulator. I am not getting any errors in the logcat or when loading the PHP file. Thanks for any help.
Below is my java code in full:
public class EventInformation extends Activity
{
private String url = "http://10.0.2.2/Web-Coding/android_get_event.php";
TextView ev_title, ev_desc;
Intent intent;
public String the_title, the_desc;
public HttpClient httpclient;
public HttpPost httppost;
StringBuilder sb1;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.eventinformation_layout);
ev_title = (TextView)findViewById(R.id.event_title);
ev_desc = (TextView)findViewById(R.id.event_description);
String text = "Hello!";
new UploadTask().execute(text);
}
private class UploadTask extends AsyncTask<String, Integer, String>
{
private ProgressDialog progressDialog;
@Override
protected void onPreExecute()
{
progressDialog = new ProgressDialog(EventInformation.this);
progressDialog.setMessage("Uploading...");
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(true);
progressDialog.show();
super.onPreExecute();
}
@Override
protected String doInBackground(String... params)
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id", "23"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null)
{
String responseStr = EntityUtils.toString(resEntity).trim();
Log.v("TAG", "Response: " + responseStr);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result)
{
if (progressDialog != null) {
progressDialog.dismiss();
}
// process the result
super.onPostExecute(result);
}
}
}
Here is the PHP code to get and display the posted data:
<?php
if($_POST)
{
print_r($_POST);
}
?>
add the following line after Log.v()
return responseStr;
This will forward the response to onPostExecute()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.