I used simple Namevaluepairs
in android to post some of my data into a localhost php-server i made before. In the meantime, i needed to post those datas in json objects and the new code was:
public class scanner extends AppCompatActivity {
private static final String TAG = "scanner log";
private Button buttonScan;
private TextView tv1, tv2;
private IntentIntegrator qrScan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scanner);
new postDataAsync().execute();
buttonScan = (Button) findViewById(R.id.buttonScan);
tv1 = (TextView) findViewById(R.id.tvprice);
tv2 = (TextView) findViewById(R.id.tvdes);
qrScan = new IntentIntegrator(this);
buttonScan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
qrScan.initiateScan();
}
});
}
public class postDataAsync extends AsyncTask<String, String, String> {
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... strings) {
try {
postData();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String lenghtOfFile) {
}
}
private void postData() {
try {
String postReceiverUrl = "http://192.168.1.107/bot/post.php";
Log.v(TAG, "postURL: " + postReceiverUrl);
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(postReceiverUrl);
List<NameValuePair> nameValuePairs = new ArrayList();
DatabaseHandler db = new DatabaseHandler(this);
db.openDB();
Cursor c = db.getAllDatas();
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = new JSONArray();
while (c.moveToNext()) {
String name = c.getString(1);
String price = c.getString(2);
//nameValuePairs.add(new BasicNameValuePair("name", name));
//nameValuePairs.add(new BasicNameValuePair("price", price));
jsonObject.put("name", name);
jsonObject.put("price", price);
jsonArray.put(jsonObject);
//httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//HttpResponse response = httpClient.execute(httpPost);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() == null) {
Toast.makeText(this, "Result Not Found", Toast.LENGTH_LONG).show();
} else {
try {
JSONObject obj = new JSONObject(result.getContents());
tv1.setText(obj.getString("name"));
tv2.setText(obj.getString("price"));
} catch (JSONException e) {
e.printStackTrace();
/*When Control Comes Over, Means You Can Show Anything Up there!*/
Toast.makeText(this, result.getContents(), Toast.LENGTH_LONG).show();
}
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
And in my localhost server i have had a php code. I can finish the code(posting json), and my php code was:
<?php
$name=$_POST["name"];
$price = $_POST["price"];
$result = '<br>' . $name . ' ' . $price;
$filename="submitted-msg.html";
file_put_contents($filename,$result,FILE_APPEND);
$androidmessages=file_get_contents($filename);
echo $androidmessages;
?>
After converting the values into json object and putting them in json array, i just wanna know how could i receive these values from the php-server, as i don't have $_POST["name"]
or whatever now! By which variable in php i should use so that i could post that json array.
Thanks in advance!