I have a Rails server, and I want my Java desktop application & android app to be able to interact with the standard scaffold (new/ edit/ show/ etc) so I can sync data between everything.
I found this (link) which shows the basic idea but not the actual code..
The catch is that the user needs to be logged in with devise, so they only see their data, not mine or yours!
Please help me regarding this.
JSON will better for android apps. Its lightweight than XML.
when you are connecting to a server. each request will be webservice call to the server. you can send the authentication in the header in Base64 encoded form. so each request is parsed in the server and the credentials can be decoded and authenticated before serving the response.
To identify the device you can send the devices IME number. you can have a table to keep track of the devices that log into your server.
check this question for detail
For the base64 authentication in the client side using json. i haven't done with xml.
public static JSONObject SendHttpPost(Context context, JSONObject jsonObjSend) {
mPrefs = AppConfig.getPreferences(context);
String username = mPrefs.getString("UserName","");
String password = mPrefs.getString("Password","");
String host = mPrefs.getString("URL","");
String port = mPrefs.getString("Port","");
String url = "http:\\myapp.com\controller\getuser"
HttpResponse response = null ;
JSONObject jsonObjRecv =null;
try {
String usercredential = Utility.getB64Auth(username, password);
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPostRequest = new HttpPost(url);
StringEntity se;
se = new StringEntity(jsonObjSend.toString());
// Set HTTP parameters
httpPostRequest.setEntity(se);
httpPostRequest.setHeader("Authorization", usercredential);
httpPostRequest.setHeader("Accept", "application/json");
httpPostRequest.setHeader("Content-type", "application/json");
long t = System.currentTimeMillis();
response = (HttpResponse) httpclient.execute(httpPostRequest);
Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis()-t) + "ms]");
//Get hold of the response entity (-> the data):
HttpEntity entity = response.getEntity();
if (entity != null) {
// Read the content stream
InputStream instream = entity.getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
instream = new GZIPInputStream(instream);
}
// convert content stream to a String
String resultString= convertStreamToString(instream);
Log.v(null, "resultString "+resultString);
instream.close();
// Transform the String into a JSONObject
if(resultString!=null){
jsonObjRecv = new JSONObject(resultString);
}
// Raw DEBUG output of our received JSON object:
Log.i(TAG,"<jsonobject>\n"+jsonObjRecv.toString()+"\n</jsonobject>");
return jsonObjRecv;
}
} catch(SocketException se){
se.printStackTrace();
}catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
Edit yes username and password pre set. use a screen like preference screen to set it. can refer json.org for parsing and creating json. yes can create nested jsons.
JSONObject body = new JSONObject();
JSONObject note = new JSONObject();
JSONObject commit = new JSONObject();
note.put("value", test2);
commit.put("create", note);
body.put("note", note);
body.put("commit", commit);
Related
I wrote both Service and CLient part of application. I tested my service with "Postman" application and it is working fine with url = http://192.168.2.50:8084/FaceBusinessService/webresources/service/login?phone=123456789&password=1234
However when I try to call it on my Android Application it is not working. While debuging on service side I see that phone and password parameters are NULL.
Here is my service side :
#Path("login")
#POST
#Produces("application/json")
public String postJson(#QueryParam("phone")String phone, #QueryParam("password") String password) {
String info = null;
try {
UserInfo userInfo = null;
UserModel userModel = new UserModel();
userInfo = userModel.isPersonRegistered(phone, password);
Gson gson = new Gson();
System.out.println(gson.toJson(userInfo));
info = gson.toJson(userInfo);
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
}
return info;
}
Here is my android app side :
private UserInfo loginUser(String phone, String password) {
UserInfo userInfo = null;
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost("http://192.168.2.27:8084/FaceBusinessService/webresources/service/login");
try {
/*
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("phone", new StringBody(phone));
entity.addPart("password", new StringBody(password));
post.setEntity(entity);
*/
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("phone", phone));
params.add(new BasicNameValuePair("password", password));
post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
Log.d(TAG, "POST String: " + post.toString());
try {
HttpResponse response = httpClient.execute(post);
if (response.getEntity().getContentLength() > 0) {
String json_string = EntityUtils.toString(response.getEntity());
JSONObject jsonObject = new JSONObject(json_string);
// TODO
return userInfo;
}
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
return null;
}
I tried both MultipartEntity and NameValuePair but none of them worked. Could you give me idea how to handle this issue?
Note that when testing with Postman you passed parameters (user name and password) as part of the URL (URL encoded), which can be directly retrieved on the server side. (you don't even need a POST request for this). Your objects are passed as string objects, not JSON objects.
In your client code , the URL is different because you're encoding the parameters as part of the POST request entity (payload). The parameters are packaged inside of the request/message body and not in the URL.
Now since your URL doesn't have the parameters, you should retrieve them by deserializing the request (desderialize the JSON request into a UserInfo object).
Note that you should rewrite your server side code completely as it should accept a application/JSON object but it apparently should return/produce a String object (plain/text or application/HTML).
I'm not familiar with GSON but your code might look something like
#Path("login")
#POST
#Produces("text/plain")
#Consumes("application/json")
public String postJson(UserInfo ui) {
String info = null;
try {
UserInfo userInfo = null;
UserModel userModel = new UserModel();
userInfo = userModel.isPersonRegistered(ui.phone, ui.password);
Gson gson = new Gson();
System.out.println(gson.toJson(userInfo));
info = gson.toJson(userInfo);
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
}
return info;
}
I want to POST an XML file as DOM Document to a php page (received using Java).
When the Object is received in the php side. I read/traverse the file as a DOM Documents and send another XML DOcument file as response to the post.
Any directions would be very much appreciated.
Here's my sample Java code..
private Document sendToServerAndFetchResponse(Document xmlDocument) {
Document responseXML = null;
// Create the httpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
// Url to which the post has to be performed.
HttpPost httppost = new HttpPost(
"http://192.168.0.19:3334/cogglrestservice.svc/InsertTrack");
// Make sure the server knows what kind of a response we will accept
httppost.addHeader("Accept", "text/xml");
// Also be sure to tell the server what kind of content we are sending
httppost.addHeader("Content-Type", "application/xml");
try {
StringEntity entity = new StringEntity(xmlDocument.toString(),
"UTF-8");
entity.setContentType("application/xml");
httppost.setEntity(entity);
// execute is a blocking call, it's best to call this code in a
// thread separate from the ui's
HttpResponse response = httpClient.execute(httppost);
BasicResponseHandler responseHandler = new BasicResponseHandler();
String strResponse = null;
if (response != null) {
try {
// Returns the response body as a String if the response was
// successful (a 2xx status code).
// If no response body exists, this returns null. If the
// response was unsuccessful (>= 300 status code), throws an
// HttpResponseException.
strResponse = responseHandler.handleResponse(response);
} catch (HttpResponseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
}
return responseXML;
}
}
DOM is available in PHP, too. Your data (the request body) should be available as stdin. Here is a simple demo:
// create a dom and load the request body
$dom = new DOMDocument();
$dom->loadXml(file_get_contents('php://stdin'));
// create an xpath instance for the document
$xpath = new DOMXpath($dom);
// look for nodes and set the attribute
foreach ($xpath->evaluate('/foo/bar') as $node) {
$node->setAttribute('attr', 'success');
}
// send the header and output the document as string
header('Content-Type: application/xml');
echo $dom->saveXml();
Demo: https://eval.in/161089
You should recognize the methods of DOMDocument from Javas xmlDocument.
When I execute an API through following method, I always get 404 as response code.
private void execute() throws IllegalStateException, IOException, NoSuchAlgorithmException {
Map<String, String> comment = new HashMap<String, String>();
comment.put("accounts-groups", "customers/enterprise");
comment.put("companyType", "customer");
comment.put("companyName", "Test");
String json = new GsonBuilder().create().toJson(comment, Map.class);
Log.i(TAG, "json : "+json);
HttpResponse response = makeRequest(URL, json);
/*Checking response */
if(response != null) {
InputStream inputStream = response.getEntity().getContent(); //Get the data in the entity
int statusCode = response.getStatusLine().getStatusCode();
Log.i(TAG, "statusCode : "+statusCode);
String result;
// convert inputstream to string
if(inputStream != null)
result = convertStreamToString(inputStream);
else
result = "Did not work!";
Log.i(TAG, "result : "+result);
}
}
private HttpResponse makeRequest(String uri, String json) throws NoSuchAlgorithmException {
Log.i(TAG, "uri : "+uri);
try {
HttpPost httpPost = new HttpPost(uri);
httpPost.setEntity(new StringEntity(json, HTTP.UTF_8));
long timestamp = System.currentTimeMillis();
String signatureKey = PRIVATE_KEY + timestamp;
byte[] bytesOfMessage = signatureKey.getBytes(HTTP.UTF_8);
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] thedigest = md.digest(bytesOfMessage);
char[] signature = Hex.encodeHex(thedigest);
String finalSignature = String.valueOf(signature);
Log.i(TAG, "finalSignature : "+finalSignature);
httpPost.setHeader("Timestamp", ""+timestamp);
httpPost.setHeader("Api_token", API_TOKEN);
httpPost.setHeader("Signature" , finalSignature);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader(HTTP.CONTENT_TYPE, "application/json");
return new DefaultHttpClient().execute(httpPost);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
I am not getting where am I going wrong. Can anybody please help me out?
from wiki:
The 404 or Not Found error message is a HTTP standard response code
indicating that the client was able to communicate with the server,
but the server could not find what was requested.
so, your code is OK, but server cannot find resource you are looking for. Double check if your url is correct.
how to pass request through fiddler proxy for debugging purposes:
HttpParams params = new BasicHttpParams();
// ....
HttpHost proxy = new HttpHost("192.168.1.12", 8888); // IP to your PC with fiddler proxy
params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
// use params as a second parameter to: following constructor:
// public DefaultHttpClient (ClientConnectionManager conman, HttpParams params)
I was getting 404 for POST requests because mod_headers module of Apache 2 server was not enabled. If that happens you can enable it with:
sudo a2enmod headers
and then restart apache:
sudo service apache2 restart
I'm trying to create new user to OpenStack using the api given in this link
http://docs.openstack.org/api/openstack-identity-service/2.0/content/POST_addUser_v2.0_users_Admin_API_Service_Developer_Operations-d1e1356.html
I have passed the token replied by the server when I logged-in.
This is my code for creating new user:
Log.i("TAG","Adding new User");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.1.122:35357/v2.0/users");
try {
JSONObject newuser = new JSONObject();
JSONObject userInfo = new JSONObject();
newuser.put("user", userInfo);
//end of JsonArray
userInfo.put("username", "Lou Mary");
userInfo.put("email", "lagojo#owtel.com");
userInfo.put("enabled", true);
userInfo.put("OS-KSADM:password", "secret101");
Log.i("TAG", "passing your data"+newuser.toString());
// erequest.setText(auth.toString());
StringEntity params1 = new StringEntity(newuser.toJSONString());
params1.setContentEncoding("UTF-8");
//params1.setContentType("application/json");
Log.i("TAG","params" +params1);
httppost.setHeader("Content-type", "application/json");
httppost.setHeader("X-Auth-Token", "MIICbgYJKoZIhvcNAQcCoIICXzCCAlsCAQExCTAHBgUrDgMCGjCCAUcGCSqGSIb3DQEHAaCCATgEggE0eyJhY2Nlc3MiOiB7InRva2VuIjogeyJpc3N1ZWRfYXQiOiAiMjAxMy0xMS0yMlQwMDo1NjoxMy42NDI2NjkiLCAiZXhwaXJlcyI6ICIyMDEzLTExLTIzVDAwOjU2OjEzWiIsICJpZCI6ICJwbGFjZWhvbGRlciJ9LCAic2VydmljZUNhdGFsb2ciOiBbXSwgInVzZXIiOiB7InVzZXJuYW1lIjogImFkbWluIiwgInJvbGVzX2xpbmtzIjogW10sICJpZCI6ICJjY2MwMjJkZGNhMzU0N2NiYmIxMmZmNTViNTZkOGI2OCIsICJyb2xlcyI6IFtdLCAibmFtZSI6ICJhZG1pbiJ9LCAibWV0YWRhdGEiOiB7ImlzX2FkbWluIjogMCwgInJvbGVzIjogW119fX0xgf8wgfwCAQEwXDBXMQswCQYDVQQGEwJVUzEOMAwGA1UECBMFVW5zZXQxDjAMBgNVBAcTBVVuc2V0MQ4wDAYDVQQKEwVVbnNldDEYMBYGA1UEAxMPd3d3LmV4YW1wbGUuY29tAgEBMAcGBSsOAwIaMA0GCSqGSIb3DQEBAQUABIGAqoSsewZ+ceYq1JXnu9OVvTJj+Aljm+rUio1biXow72iZ+MVBJKbKvlT4-2DFPC1PrCOErpX2jJ7HuiASSaBgAcROT+LmV3KNnHa+p9DCtgSBGRN7qHJpnQBXgs3tz4ZMVi3AB9i1mOmVHxeVKVfiQWt1zyis7OZPG-PZRq1DohQ=");
httppost.setEntity((params1));
// Execute HTTP Post Request
Log.i("TAG", "pushing your data");
HttpResponse response = httpclient.execute(httppost);
Log.i("TAG", "Sucessful " + response.getParams());
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String jsonresponse = reader.readLine();
JSONTokener tokener = new JSONTokener(jsonresponse);
try {
JSONArray finalResult = new JSONArray(tokener);
Log.i("TAG", "Sucessfully communicated on server");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}catch (IOException e) {
Log.e("", "IOException " + e.toString());
Log.i("", "The server refused again! ");
// TODO Auto-generated catch block
}
}
But I'm getting this Error
org.json.JSONException: Value{
"error": {
"message": "The request you have made requires authentication.",
"title": "Not Authorized",
"code": 401
} }
I'm expecting that the token I've passed will authorize me from adding new user.
That's why I don't understand the error.
Anyone, please help. Any idea how to solve this?
Apart from checking that you have the Admin role for the target project (e.g. when you request the auth token, you need to specify the tenant ID), also make sure that the auth token is not expired.
Also I believe you should use
"name", "Lou Mary"
instead of
"username", "Lou Mary"
I'm tearing my hair out over this problem I am having. I am trying to allow a user to upload some data from their android application to a website service which I have developed.
The data is to be uploaded using JSON and Android to a PHP web service which will then 'INSERT' the data into my PostgreSQL database.
I am unsure where the logic error is in my whole application as the app produces no errors at runtime but when I check the database records of my PostgreSQL server space there has been no data committed.
Please see below the code I am using and please try to help identify where I am going wrong. I have looked for tutorials on Google but they all are based on reading data FROM a PHP web service to an android app but I am looking to send the original data from the android app.
DataPost Activity
public void postData() throws JSONException{
Toast.makeText(DataSummary.this, "Done! Check your profile online to see your record.", Toast.LENGTH_LONG).show();
Thread trd = new Thread(new Runnable(){
public void run(){
//Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://users.aber.ac.uk/dwd/mfb/php/jsonscript.php");
JSONObject json = new JSONObject();
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), i);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte[] ba = bao.toByteArray();
String ba1=Base64.encodeToString(ba, i);
try {
//JSON data:
json.put("photo", ba1.toString());
json.put("name", name);
json.put("description", description);
json.put("latitude", latitude);
json.put("longitude", longitude);
json.put("project", project);
json.put("owner", username);
JSONArray postjson = new JSONArray();
postjson.put(json);
//Post the data
httppost.setHeader("json", json.toString());
httppost.getParams().setParameter("jsonpost", postjson);
//Execute HTTP Post Request
System.out.println(json);
HttpResponse response = httpclient.execute(httppost);
//for JSON
if(response != null)
{
InputStream is = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try{
while((line = reader.readLine()) != null){
sb.append(line + "\n");
}
} catch (IOException e){
e.printStackTrace();
} finally {
try {
is.close();
} catch(IOException e){
e.printStackTrace();
}
}
}
} catch(ClientProtocolException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
trd.start();
}
PHP Webservice
<?php
session_start();
$conn = pg_connect("database_string");
//VARIABLES TO BE WRITTEN TO THE DATABASE
$photo = $_REQUEST["photo"];
echo $photo;
$binary=base64_decode($photo);
header('Content-Type: bitmap; charset=utf-8');
$name = json_decode(stripslashes($_POST["name"]));
$safe_name = pg_escape_string($name);
$desc = json_decode(stripslashes($_POST["description"]));
$safe_desc = pg_escape_string($desc);
$latitude = json_decode(stripslashes($_POST["latitude"]));
$longitude = json_decode(stripslashes($_POST["longitude"]));
$project = json_decode(stripslashes($_POST["project"]));
$owner = json_decode(stripslashes($_POST["owner"]));
$id = pg_query("SELECT * FROM users WHERE email = $owner");
$id_assoc = pg_fetch_assoc($id);
$id_res = $id_assoc['u_id'];
//SQL STATEMENT HERE FOR INSERT
$res = pg_query("INSERT INTO records (photo, name, description, latitude, longitude, project, owner) VALUES ('$photo', '$safe_name', '$safe_desc', '$latitude', '$longitude', '$project', '$id_res'");
pg_close($conn);
?>
Anyone who can provide some advice/tutorials/code solutions would be a hero in my book!
Does the SELECT query return anything? I'm not a PHP expert but to me it looks like you're sending the variables wrong so there shouldn't be:
$id = pg_query("SELECT * FROM users WHERE email = $owner");
But
$id = pg_query("SELECT * FROM users WHERE email ='".$owner."'");
Similar for the INSERT query.
Other thoughts:
don't do a SELECT * when you just want one column it will be slower. For example with index-only-scans in 9.2 you could return the id straight from the index(email,id)
if you want to use just the id of the user it's better to put it in the subquery of the insert query
INSERT INTO records ( ... ,owner) VALUES (... ,(SELECT id FROM users WHERE email='".$owner."')")
You could even add RETURNING owner at the end to get the owner id out from the insert query if you need it somewhere else.