Firstly, I’m receiving the status code 200. In the php script I’m just having the code mail me whats being sent to the server (whilst I try and debug). Using var_dump $_POST or $_REQUEST I get a blank email. If I run a foreach on $_SERVER I get some data back like HTTP_USER_AGENT : Apache-HttpClient/UNAVAILABLE (java 1.4) and CONTENT_LENGTH : 9
I don’t understand why there’s no post data? I’m new to android, I’m literally learning as I go.
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(dest_url);
//httppost.setHeader("Accept", "application/json");
//httppost.setHeader("Content-type", "application/json");
//JSONObject json = new JSONObject();
// Add your data
//json.put("action", "login");
//json.put("username", "kermit");
//json.put("password", "123456");
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//nameValuePairs.add(new BasicNameValuePair("myjson", json.toString()));
nameValuePairs.add(new BasicNameValuePair("plswork", "hi"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
//StringEntity str_json = new StringEntity(json.toString());
//httppost.setEntity(str_json);
HttpResponse response;
response = httpclient.execute(httppost);
My manifestxml has the following line;
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
These are my imports;
import java.io.IOException;
import java.util.ArrayList;
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 org.apache.http.protocol.HTTP;
import org.json.JSONObject;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
Most likely it has something to do with your
application/jsonis not a normal content type for HTTP POST form submission. Considering that you’re not really sending json but rather a standard POST entity (nameString=valueString), try changing this toand see whether this will sort your issue. Or better yet, remove it alltogether.
I use this code in my app – and it works perfectly fine:
The response in this case is
text/xml– but this shouldn’t matter.