Reputation: 11
I want to perform this query on a PHP server from my Android device:
{
"command": "REGISTER",
"data": {
"email": "EMAIL",
"login": "LOGIN",
"password": "PASSWORD",
"language": "USER_LANGUAGE"
}
public class AysnchronousTaskPost extends AsyncTask<String, Long, String> {
private static final String PATH = "http://alphabravo.com/myapp/api.php";
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
InputStream mInputStream;
String lineString = "";
String resultString = "";
StringBuilder mStringBuilder = null;
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
nameValuePairList.add(new BasicNameValuePair("command","register"));
nameValuePairList.add(new BasicNameValuePair("email","[email protected]"));
nameValuePairList.add(new BasicNameValuePair("login", "abc"));
nameValuePairList.add(new BasicNameValuePair("password", "1205"));
nameValuePairList.add(new BasicNameValuePair("language", "en"));
try {
HttpClient mHttpClient = new DefaultHttpClient();
HttpPost mHttpPost = new HttpPost(PATH);
mHttpPost.setEntity(new UrlEncodedFormEntity(nameValuePairList));
HttpResponse mHttpResponse = mHttpClient.execute(mHttpPost);
HttpEntity mHttpEntity = mHttpResponse.getEntity();
StatusLine mStatusLine = mHttpResponse.getStatusLine();
int statusCodeString = mStatusLine.getStatusCode();
if (statusCodeString == 200) {
mInputStream = mHttpEntity.getContent();
mStringBuilder = new StringBuilder();
BufferedReader mBufferedReader = new BufferedReader(
new InputStreamReader(mInputStream, "iso-8859-1"), 8);
while ((lineString = mBufferedReader.readLine()) != null)
mStringBuilder.append(lineString + "/n");
}
resultString = mStringBuilder.toString();
} catch (Exception e) {
// TODO: handle exception
}
return resultString;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
I am getting an empty BufferedReader.ReadLine()
. Am I creating mHttpPost
correctly?
I have Also Tried HTTPget
Method.
but the result is same.Please Help me
I have Checked my php Script on Browser.My PHP Script is working fine .
Upvotes: 0
Views: 767
Reputation: 3666
You need to convert your key-value pair into content-type application/json and then send it to your server.
Try this in asynctask,
JSONObject data = new JSONObject();
// Add key/value pairs
data.put("email", email);
data.put("login", login);
data.put("password", password);
data.put("language", language);
JSONObject json = new JSONObject();
json.put("command", command);
json.put("data", data);
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPostRequest = new HttpPost("http://alphabravo.com/myapp/api.php");
StringEntity se;
se = new StringEntity(json.toString());
// Set HTTP parameters
httpPostRequest.setEntity(se);
httpPostRequest.setHeader("Accept", "application/json");
httpPostRequest.setHeader("Content-type", "application/json");
HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
HttpEntity entity = response.getEntity();
String receiveJson = EntityUtils.toString(entity);
//use receiveJson as new JSONObject(receiveJson) ot new JSONArray(receiveJson) as per your returned json value
Upvotes: 2