Send JSON from Android device to server not working - android

I have two links :
private static final String url_to_reg_device1 = "http://10.0.2.2/android_connec/regdevice.php";
private static final String url_to_reg_device2 = "http://www.something.in/form/regdevice.php";
One is for localhost and another is for server.
When I start activity, I create one JSON which store email and reg number and send it to server. On the server side, I decode JSON record and get values, store it in db.
The problem is that my code is working on local server. But when I upload it on server (to the URL in url_to_reg_device2), I got this error:
"Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject".

Check tags of JSON Object properly.

Related

How to send Base64 String as a parameter to api

i am working on a android project where i have to send a base64 converted image string to server and retrive but in my case the url is like below
i am able to convert the string but the string is too big and i am unable to pass that big string into the url. Is there any way i can pass this string to url as parameter?

How retrieve data from websocket?

Please how i can retrieve data and username from web socket,
io.sockets.emit('update chat', 'username':socket.username, 'data':data);
i'm sending data from android to socket then i want retrieve this data in my server chat but i get [object object],
You may need to use JSON.stringify before emiting the data variable like
io.sockets.emit('update chat'
, 'username':socket.username
, 'data':JSON.stringify(data));
and parse same string back to json form at recieving end via JSON.parse like
var objData = JSON.parse(data);

Ajax post data in android java

I am new t ajax, but quite familiar with android. I am converting a ajax program to android app. As a part of it, i need to post data to the server. Below is the given post command in ajax.
var postTo = 'xyz.php';
$.post(postTo,{employee_name: $('[name=employee_name]').val() , phone: $('[name=phone]').val(), employee_type: 'guest' } ,
function(data) {
if(data.success){
window.localStorage["sa_id"] = data.mid;
window.location="getempdb.html";
}
if(data.message) {
$('#output').html(data.message);
} else {
$('#output').html('Could not connect');
}
},'json');
I want to implement this in android but under very little from the above statements. Could anyone who is good at ajax help me out with this thing. As of now, i get the user name and telephone number as a edit text input. I need to send this to php using http client. I know how to send data using php, but do not know what format to send and whether its a string to send or as a json object to send. Please help in interpreting the above code and oblige.
Apparently, this uses UrlEncodedFormEntity if you are using HttpClient in android.
This is created by using a List of NameValuePair.
from the parameters to the $.post:
{employee_name: $('[name=employee_name]').val() , phone: $('[name=phone]').val(), employee_type: 'guest' }
You have to create a NameValuePair for employee_name, one for phone ... each of which is fetched from a HTML element name employee_name, phone ... This is where you put the values from your EditTexts.
It returns a JSON formatted String, which you have to parse (typically using JSONObject obj = new JSONObject(result); once you have fetched the result from the server)
In this JSON object, you have a key named success, which format is not specified, except you can assume things went well if it is present ; a key mid, and a key message.

How to remove the special characters when getting the data using json in android?

I am working on android application. I need to get the data from PHP web service. The response which I am getting from PHP server is shown below:
String response = [{id:100,category:local,count1:58,count2:86},
{id:101,category:server/local,count1:18,count2:27},
{id:102,category:server & local,count1:19,count2:28}];
But I am getting the following message when I am trying to separate the data using JSON.
org.json.JSONException: Unterminated object at character
How to remove special characters and get the data using JSON array?
Strings should be under double quotes. Your json is not a valid response. So change your response from
{id:100,category:local,count1:58,count2:86}
to
{
"id":100,
"category":"local",
"count1":58,
"count2":86
}
Refer here
Valid JSON format
{
"Status":"OK",
"Error":{
"Code":0,
"Description":""
},
"Data":[]
}

Sending response from a server android

my code sends latitude a longitude to server as parameters to a servlet.
In server it calculates the nearest distance to a branch and should send back an whole data of information like City Name, Address, latitude and longitude of branches. I have all of these information in database in server and I am also getting ordered list upon distance.
But how to send this list as an response from server to a device and how shall I collect this data from the response in android. Any help with code example would be helpful. thanks.
at the server side you need to create a class with name resulatanClass & make all the datamember that you are going to return. now return this class in your response. Or the alternate way is you can send them in XML format & at the android app side you need to parse then while receiving it.
You should try creating a web service for this. A web service is like a public function that you can call over the net. The response of the web service can be in XML form. The android device must connect to the web service and wait for its response and then parse the response accordingly.
A web service has its own link so it's just like connecting to a URL and waiting for its response.
Sample Web Service Call:
httpURLConnection = (HttpURLConnection) ((new URL("http://webServiceURL/webServiceMethod")).openConnection()); //connect to the url of the web service
console = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream())); //get the response of the web service
Sample Web Service Method:
public String webServiceMethod(String argumento)
{
String response;
//set response value here depending on the value of the parameter
return response; //yes, returning a response in web service is as straightforward as this
}
I would use google-gson for that.
If you just want to send a simple object you can do the following:
1: create an object that holds the data you want to transfer
class MyDataObject {
private String cityname, address;
private double longitude, latitude;
MyDataObject() {
// no-args constructor
}
}
2: create your JSON response string that you send back in your HTTP response
MyDataObject data = new MyDataObject();
// set values
Gson gson = new Gson();
String responseData = gson.toJson(data);
// put this string in your response
3: read response on your Android client
String responseData;
// read response string
Gson gson = new Gson();
MyDataObject data = gson.fromJson(responseData,MyDataObject.class);
// access the data stored in your object
You can also send arrays or other more complex objects using JSON. If you want to use google-gson for this you should take a look at GSON User Guide
-axel
I suggest you build a JSON string which will contain all the information and do a HTTP POST request from the android and get back the results.
Using the data retrieved from the server parse the JSON and use the data you need in the views.

Categories

Resources