How can I establish a connection between my Android app and a PHP server using HTTPUrlConnection to send data in the output stream and ensure that the server receives it in the $_

When using HttpURLConnection to send a JSON query to a PHP server, I encountered issues with the communication not working as expected.

Despite having a successful connection and receiving proper error responses from the server side, it seems that the PHP server is unable to retrieve the JSON string in the variable $_POST. The JSON data sent looks like this:

{"id":55,"date":"2011111","text":"","latitude":13.0,"longitude":123.0,"share":0,"image":"Image","sound":"sound"}

On the Android side, the code snippet used is quite straightforward:

    // Code snippet

The PHP server-side code appears as follows:

// Server code

Upon conducting some research, I discovered a workaround that involves modifying the server-side code. By making adjustments such as extracting the JSON data from 'php://input', the PHP server can now effectively handle the JSON information received from the Android application.

Unfortunately, the challenge lies in the inability to modify the code on the actual server. This limitation raises questions about why the $_POST variable is not being populated appropriately.

Answer №1

Your data is being transmitted in JSON format, but not within the $_POST array, which is why $_POST appears empty. If you are unable to modify the server-side code, consider using my HttpConnection class as an alternative solution. This should help resolve the issue.

import org.json.JSONException;
import org.json.JSONObject;

// Code for HttpConnection class goes here

To make a POST request, simply utilize the sendRequest method by providing the link and the data to be sent using a HashMap.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Outdated authentication token in PHP Facebook software development kit

I recently started using the latest version of Facebook's PHP SDK (4.*) and ran into a little issue! ;) Essentially, I'm trying to figure out how to send requests to the Graph API after the initial redirect when the user is redirected back to my ...

Angular directive dilemma

Angular is causing some issues for me as I am a beginner in using it. Below is the JSON data that I am dealing with: [ { "name":"43", "values":{ "audio":"Audio Only", "low":"Low Bandwidth", "medium":"Medium Bandw ...

Convert JSON response date format to a format specified by the user

The following code snippet is currently returning the dates for $("#dob") and $("#anniversery") as 2014-04-01T00:00:00 This is my current code: <script> $(function() { function log(message) { $("<div>").text(message).p ...

Transforming text into numbers from JSON response with *ngFor in Angular 6

I'm currently attempting to convert certain strings from a JSON response into numbers. For instance, I need to change the zipcode value from "92998-3874" to 92998-3874. While doing this, I came across a helpful resource on Stack Overflow. However, I s ...

Node.js JSON parser encountering an unexpected undefined error

Whenever I attempt to JSON.parse the string, an error occurs and I receive this message: undefined:1 {"device":"FaclonTest","data":[{"tag":"LATITUDE","value":1903.5091},{"tag":"LONGITUDE","value":07251.0348}]} I'm unsure of where the mistake is. Can ...

Error in accessing the value from the JSON response

After uploading a photo to an external cloud CDN, I receive a JSON response containing relevant information about the uploaded photo. One key piece of data is the public_id field, which I need to store in my database. The response structure is as follows: ...

Eliminate duplicate entries from various JSON Arrays

I am facing an issue with a JSON structure containing multiple arrays. My goal is to identify and eliminate any duplicate values across different arrays while keeping the other fields intact. Here is a sample of the structure I am working with: { "Coll ...

Configurations for MongoDB document serialization

Is it possible to serialize only private fields of an object with DataMember attributes in MongoDB? string json = item.ToJson( new MongoDB.Bson.IO.JsonWriterSettings() { GuidRepresentation = GuidRepresentation.Standard ...

Retrieving cascading values in a dropdownlist using Asp .NET MVC

In my current project, I am using Asp.Net MVC4. In a form, I have implemented cascading dropdown lists. When I select "Distrito" in one dropdown list, it correctly loads the corresponding values for "Sucursal". The insertion of records works fine. However, ...

Unable to retrieve properties post JSON deserialization into dynamic

Struggling with accessing dynamic properties post JSON deserialization. Take a look at the JSON data: { "user" : { "511221" :{ "timestamp" : 1403365646384, "version" : -81317051, "email" : "<a href="/cdn-cgi/l/email-p ...

Is it possible to automatically select a file and upload it to a specific server? If so, how can this

Hey there! I'm looking for a code snippet that allows me to automatically select a file and upload it to a specific link. Any ideas on how to accomplish this task? Here is the HTML code: <html> <head><title>Uploading</title>& ...

Guide on accessing public post information through Instagram Graph API using PHP

Seeking guidance on initiating the use of the Graph API to fetch fundamental public details from an Instagram account, such as account name, likes, post date, image URL, and description. While I successfully accomplished this task with the Instagram API, ...

Why does my JSON parser consume significantly more memory when utilizing the Streaming API?

My current code is parsing the whole file using jsonparse: ConcurrentHashMap<String, ValueClass<V>> space = new ConcurrentHashMap<String, ValueClass<V>> (); Map<String, ValueClass<V>> map = mapper.readValue(new FileRead ...

js issue with passing form data to use with PHP mail function

As a novice, I am diving into the world of HTML webpage creation. Utilizing a free online template, my current project involves developing a Contact Page that triggers a php script to send an email with the captured fields. While I've successfully man ...

Keep only the PHP characters intact while replacing all others in the given string

I need help with extracting the year 1988 from a string that says "Trade Card Catalogue 1988 Edition". Is there a more efficient solution instead of using an array, str_replace, and trim? $string = 'Trade Card Catalogue 1988 Edition'; $letters = ...

Learning how to interpret a JSON string in VB6

I am currently working on a project in VB6 where I need to call a web service that returns a JSON string as a response. After successfully storing the response in a string, my next task is to extract each parameter individually. Could someone provide gui ...

When the json string contains more bytes than visible characters, the json_decode() function may encounter errors

It seems like there's an issue with this code snippet: $jsonDecode = json_decode($jsonData, TRUE); Surprisingly, when I manually place the string from $jsonData inside the decode function, it works perfectly fine. This solution does work: $jsonDecod ...

Guidelines on transforming a Java Object list into JSON format and displaying it through AJAX requests

Currently, I am facing an issue where I have a Java object list retrieved from the database as an ArrayList of objects. I need to display this on a JSP page using AJAX. While I know how to display an ArrayList of Strings via AJAX in a JSP page, I'm st ...

Exception being raised despite handling in JQuery AJAX post request

I am facing an issue with my login JSON service that handles username and password. When I make a call using JQuery 1.10.1 AJAX and input the correct credentials, it functions correctly. However, if I input incorrect credentials, the error function handler ...

How to enhance a jQuery tab control by implementing custom JavaScript forward and back arrows?

Although there are other questions related to this topic, mine is unique. I have a scrolling jQuery feature that utilizes tabs for selection and is automated. Modifying the layout is challenging because it is dynamic and depends on the number of items in t ...