I am building a registration page for the users of my Android app. The UI asks for the mobile no., age and username. On entering these details, I want to send this data to my Apache server where I have written a PHP script to handle the received data. I have used JSON to send the data from the app but I am unable to receive the data at my server on running the script. It gives the error "Undefined index: json" on the lines in the script wherever i have written json
On running the app on my android-based mobile, it accepts the details entered and shows them in the UI for registration as toasts. I have used toasts to verify that the data is being actually captured by the app or not. I feel there is a problem with the PHP script as the app doesn't give any runtime exceptions as well. It runs fine.
I have searched all over the net but haven't found a solution to this.
The java code for the registration page is as follows:
package com.example.registertest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
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.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
public class Register extends Activity {
TextView tv;
String text;
private static final String TAG = "MyActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
Intent intent = getIntent();
//String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
Bundle b= this.getIntent().getExtras();
String username=b.getString("username");
String contact=b.getString("contact");
String ag=b.getString("age");
int con=Integer.parseInt(contact);
int age=Integer.parseInt(ag);
Toast.makeText(getApplicationContext(), username,
Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), contact,
Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), ag,
Toast.LENGTH_SHORT).show();
JSONObject json=new JSONObject();
try {
json.put("username", username);
json.put("contact", con);
json.put("age", age);
postData(json);
} catch (JSONException e) {
e.printStackTrace();
}
}
public void postData(JSONObject json) throws JSONException {
HttpClient httpclient = new DefaultHttpClient();
String url = "http://My_I.P./scriptname.php";
try {
HttpPost httppost = new HttpPost(url);
List<NameValuePair> nvp = new ArrayList<NameValuePair>(2);
nvp.add(new BasicNameValuePair("json", json.toString()));
httppost.setHeader("Content-type", "application/json");
httppost.setEntity(new UrlEncodedFormEntity(nvp));
HttpResponse response = httpclient.execute(httppost);
if(response != null) {
InputStream is = response.getEntity().getContent();
// input stream is response that can be shown back on android
}
}catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.register, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The PHP script is as follows:
<?php
$jsonstring = urldecode($_REQUEST['json']);
$jsonstring = $_REQUEST['json'];
$data = json_decode($json);
$data = json_decode($json,true);
if( $data === NULL)
{
exit( 'Could not decode JSON');
}
echo "Array: \n";
echo "--------------\n";
var_dump($data);
echo "\n\n";
$name = $data->username;
$con = $data->contact;
echo "Result: \n";
echo "--------------\n";
echo "Name : ".$name."\n Position : ".$con;
?>